3 ms·
Well, most early returns are avoided mostly because it makes it harder to understand the code. But when you use guard clauses, like on your Linux Kernel example
by glassx 14y ago
Well, most early returns are avoided mostly because it makes it harder to understand the code. But when you use guard clauses, like on your Linux Kernel example, you actually improve the code, because you reduce cyclomatic complexity (everything is now a linear path: a -> b -> c), you can reduce nesting, you can avoid unnecessary mutable state... so it's a win. The problem with early returns is when you use them in non-obvious places.
I believe that the majority of programmers agrees that using early returns for guard clauses is an okay exception.
- dllthomas 14y agoYes, I think really "avoid early returns" is an overextended special case of "avoid the nonobvious".