Re:“No-else-after-return” considered harmful
by makecheck on Sunday August 30, 2009 @11:23PM
(#795353)
Functions that have become too complicated should look ugly, so that they're likely to be split into simpler parts (for true readability).
When making that split, it is also helpful to have relevant cleanup code nearby: odds are everything is already in the inner block, instead of having to remove extra code at the end of the function.
Re:“No-else-after-return” considered harmful
by antonovka on Monday August 31, 2009 @12:45PM
(#796248)
That's the usual hand-waving argument (if you need this you're doing it wrong), but the fact is that even something with three simple preconditions is quickly rendered unreadable through three nested levels of if statements.
> if you need to perform cleanup, then instead of returning, use goto to jump to a single end-of-function cleanup handler.
That assumes that all of the cleanup is the same and that all of the relevant information that is available at the subroutine's top level.
Without early return, you wind up with multiple-nested if statements that make the code absolutely unreadable. Early return is almost always used as the error path -- if you need to perform cleanup, then instead of returning, use goto to jump to a single end-of-function cleanup handler.
There's nothing wrong with using goto for this purpose, and it means I can actually read -- at a glance -- code that requires more than a few preconditions at the start of the function.