Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the cleanest way to write this if..then logic?

They both do the same thing. Is one way better? Obviously if I write the code I'll know what I did, but how about someone else reading it?

if (!String.IsNullOrEmpty(returnUrl))
{
    return Redirect(returnUrl);
}
return RedirectToAction("Open", "ServiceCall");

OR

if (!String.IsNullOrEmpty(returnUrl))
{
   return Redirect(returnUrl);
}
else
{
    return RedirectToAction("Open", "ServiceCall");
}
like image 345
Mike Roosa Avatar asked Jan 29 '09 21:01

Mike Roosa


People also ask

How do you write an if then code?

An if statement checks a boolean value and only executes a block of code if that value is true . To write an if statement, write the keyword if , then inside parentheses () insert a boolean value, and then in curly brackets {} write the code that should only execute when that value is true .

How do you write better if-else?

Using if else chaining some time looks more complex, this can be avoided by writing the code in small blocks. Use of conditional statement increases the code readability and much more. One best practice should be handling error case first.


9 Answers

return String.IsNullOrEmpty(returnUrl) ? 
            RedirectToAction("Open", "ServiceCall") : 
            Redirect(returnUrl);

I prefer that.

Or the alternative:

return String.IsNullOrEmpty(returnUrl)  
            ? RedirectToAction("Open", "ServiceCall")  
            : Redirect(returnUrl);
like image 66
Andrew Rollings Avatar answered Oct 14 '22 01:10

Andrew Rollings


I believe it's better to remove the not (negation) and get the positive assertion first:

if (String.IsNullOrEmpty(returnUrl))
{
   return RedirectToAction("Open", "ServiceCall");
}
else
{
    return Redirect(returnUrl);
}

-or-

// Andrew Rollings solution
return String.IsNullOrEmpty(returnUrl) ? 
                    RedirectToAction("Open", "ServiceCall") : 
                    Redirect(returnUrl);
like image 45
Gavin Miller Avatar answered Oct 14 '22 00:10

Gavin Miller


One style issue:

if (String.IsNullOrEmpty(returnUrl))
{
    return RedirectToAction("Open", "ServiceCall");
}
return Redirect(returnUrl);

When you cancel out the double negation it reads a whole lot better, no matter which brace style you choose. Code that reads better is always best ;)

like image 29
krosenvold Avatar answered Oct 14 '22 00:10

krosenvold


the second way is better, no confusion about what you mean...

like image 40
Scott Evernden Avatar answered Oct 14 '22 01:10

Scott Evernden


I think this is a fairly small matter of style. I'd argue that your two samples are equally readable.

I prefer the former, but other people prefer only one exit point from a function and would probably suggest something like:

if (!String.IsNullOrEmpty(returnUrl))
{
   result = Redirect(returnUrl);
}
else
{
    result = RedirectToAction("Open", "ServiceCall");
}

return result;
like image 25
Dana Avatar answered Oct 13 '22 23:10

Dana


I like the first example because it's more obvious that this excerpt will return. If both returns are in indented blocks, it takes just a little more mental effort to tell.

like image 32
recursive Avatar answered Oct 14 '22 01:10

recursive


Eliminate the 'double-negative' and use the fully-expanded style. I am sure most compilers now can suitably optimize the code on your behalf, so no reason to take short-cuts for readability.

like image 45
user60400 Avatar answered Oct 14 '22 01:10

user60400


If it's the entirety of the method then I'd say the second (using the else) is a bit more elegant. If you have preceding code or (especially) much more code before the return in the else case, I'd say it's better not to put the else. Keeps from code becoming too indented.

i.e. either:

void myfunc()
{
    if (!String.IsNullOrEmpty(returnUrl))
    {
        return Redirect(returnUrl);
    }
    else
    {
        return RedirectToAction("Open", "ServiceCall");
    }
}

or

void myfunc()
{
    // ... maybe some code here ...

    if(!String.IsNullOrEmpty(returnUrl))
    {
       return Redirect(returnUrl);
    }

    // ... a bunch of other code ...

    return RedirectToAction("Open", "ServiceCall");
}
like image 39
Owen Avatar answered Oct 14 '22 01:10

Owen


This code feels messy because of the unavoidable double-negative logic (and shuffling things around isn't going to clear it up). Whichever arrangement you use, I think you should add some comments so that the reader doesn't need to do a double-take:

if (!String.IsNullOrEmpty(returnUrl))
{
  // hooray, we have a URL
  return Redirect(returnUrl);
}
else
{
  // no url, go to the default place
  return RedirectToAction("Open", "ServiceCall");
}
like image 21
too much php Avatar answered Oct 13 '22 23:10

too much php