If you're breaking an if
statement across more than one line, then for Turing's sake use brackets! So:
if (x < 10) return;
Or
if (x < 10) {
return;
}
But not
if (x < 10)
return;
The else
issue is subjective, but my view on it is that an if
/else
is conceptually breaking your code into more-or-less equal cases. Your first example is handling two conceptually similar but mutually exclusive cases, so I find an else
to be appropriate. With two separate if
statements, it appears at first glance as though the two conditionals are independent, which they are not.
When you return
on an if
, the circumstances are different. The cases are automatically mutually exclusive, since the return
will prevent the other code from running anyway. If the alternative case (without the return) is short, and escecially if it returns as well, then I tend to use the else
. If, as is common, the alternative code is long or complex, then I don't use an else
, treating the if
as more of a guard clause.
It's mostly a matter of clarity and readability, and both of those factors are subjective.