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");
}
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 .
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.
return String.IsNullOrEmpty(returnUrl) ?
RedirectToAction("Open", "ServiceCall") :
Redirect(returnUrl);
I prefer that.
Or the alternative:
return String.IsNullOrEmpty(returnUrl)
? RedirectToAction("Open", "ServiceCall")
: Redirect(returnUrl);
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);
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 ;)
the second way is better, no confusion about what you mean...
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;
I like the first example because it's more obvious that this excerpt will return. If both return
s are in indented blocks, it takes just a little more mental effort to tell.
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.
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");
}
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");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With