Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only one return statement per method, even in this scenario?

Tags:

methods

return

I like the idea of having only one return statement per method.

What do you do in this situation though?

public static string ChopText(string Text)
{
   if (String.IsNullOrEmpty(Text))
   {
      // return here ?????
   }
}

The only alternative I can think of is setting a flag, and then checking for the flag.

Problem is, I don't like having huge if statements that span more than a page. I've also seen ugly nested if statements as a result of this.

like image 736
Blankman Avatar asked Nov 24 '08 21:11

Blankman


1 Answers

It is OK to replace nested conditional with guard clauses.

like image 95
Marko Dumic Avatar answered Oct 31 '22 18:10

Marko Dumic