Here is a very simplified example. This isn't necessarily a language-specific question, and I ask that you ignore the many other ways the function can be written, and changes that can be made to it.. Color is of a unique type
string CanLeaveWithoutUmbrella()
{
if(sky.Color.Equals(Color.Blue))
{
return "Yes you can";
}
else
{
return "No you can't";
}
}
A lot of people I've met, ReSharper, and this guy (whose comment reminded me I've been looking to ask this for a while) would recommend refactoring the code to remove the else
block leaving this:
(I can't recall what the majority have said, I might not have asked this otherwise)
string CanLeaveWithoutUmbrella()
{
if(sky.Color.Equals(Color.Blue))
{
return "Yes you can";
}
return "No you can't";
}
Question: Is there an increase in complexity introduced by not including the else
block?
I'm under the impression the else
more directly states intent, by stating the fact that the code in both blocks is directly related.
Additionally I find that I can prevent subtle mistakes in logic, especially after modifications to code at a later date.
Take this variation of my simplified example (Ignoring the fact the or
operator since this is a purposely simplified example):
bool CanLeaveWithoutUmbrella()
{
if(sky.Color != Color.Blue)
{
return false;
}
return true;
}
Someone can now add a new if
block based on a condition after the first example without immediately correctly recognizing the first condition is placing a constraint on their own condition.
If an else
block were present, whoever added the new condition would be forced to go move the contents of the else
block (and if somehow they gloss over it heuristics will show the code is unreachable, which it does not in the case of one if
constraining another).
Of course there are other ways the specific example should be defined anyways, all of which prevent that situation, but it's just an example.
The length of the example I gave may skew the visual aspect of this, so assume that space taken up to the brackets is relatively insignificant to the rest of the method.
I forgot to mention a case in which I agree with the omission of an else block, and is when using an if
block to apply a constraint that must be logically satisfied for all following code, such as a null-check (or any other guards).
else
is easier to read and understand. The compiler may well choose to rearrange the code into the non-else version under the covers, and that's fine. Our job as developers is to write the clearest, most-easily-understood code possible, which in this limited case means use theelse
, Luke! – Bob Jarvis - Слава Україні Sep 06 '14 at 14:27else
block. Even the most naive compiler or interpreter would have no reason to do anything that would would have any measurable effect on the code. (I wouldn't even see those not-so-great compilers you sometimes see for smaller MCUs doing that) – Selali Adobor Sep 06 '14 at 14:37else
clause (to my taste, it will be even more readable when leaving out the unneccessary brackets. But I think the example is not well choosen, because in the case above I would writereturn sky.Color == Color.Blue
(without if/else). An example with a different return type than bool would probably make this clearer. – Doc Brown Sep 06 '14 at 16:03if
statements 100 levels deep to compare a string one letter at a time is adding complexity? It does only take basic language constructs... – Selali Adobor Sep 06 '14 at 19:48(sky.Color == Color.Blue)
and then convert it to a string when it gets used. I'd suggest an example functionfirstIndex
which takes a boolean parameterzeroIndexed
and returns an integer (0 if true, 1 if false). – Michael Shaw Sep 07 '14 at 05:51