3 ms·
Yuueurrgh. Without early return, you wind up with multiple-nested if statements that make the code absolutely unreadable. Early return is almost always used as
by antonovka 17y ago
Yuueurrgh.
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.
- makecheck 17y agoFunctions 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.
- antonovka 17y agoThat'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.
- anamax 17y ago> 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.