
Matthew Mundell wrote:
The `else' repeats the information implied by the final return or goto.
But the `else' makes it clearer that we are talking about mutually exclusive conditions. And it is also more robust: what happens if someone changes the "then" branch without noticing that you removed the `else'?
Adding the else makes it less clear that the body of the "then" branch of the `if' always ends in a return or goto.
If, in addition, you have something of the form
if (a) ... else // comment return ...
Did you mean this?
if (a) return ... else // comment ...
No, I meant what I wrote: in the original version, `else' and the comment are part of the same "phrase". If you take out the `else' then the comment assumes an "unconditional flavor" that can only be recovered by noticing that the "then" branch forces a certain control-flow. Taking out the `else' makes two sections of code that could/should be indepedent from one another strongly coupled: if you want to keep correctness you must make sure the "then" branch has the property to go somewhere else at the end.
To come back to the subject: if you only look at semantics, it is true that such elses are redundant; but if you look at the big picture (and this includes readability) things are more complex. Please notice that I am not arguing in favor of using this kind of elses everywhere: I only question that semantic redundancy is a good reason to take a random `else' and remove it. Whence my original question about the meaning of "redundant else". Cheers,
Roberto