| ▲ | bboygravity 3 days ago | ||||||||||||||||||||||||||||
Why is "no early returns" not a good rule? I do early returns in code I write, but ONLY because everybody seems to do it. I prefer stuff to be in predictable places: variables at the top, return at the end. Simpler? Delphi/Pascal style. | |||||||||||||||||||||||||||||
| ▲ | jandrewrogers 2 days ago | parent | next [-] | ||||||||||||||||||||||||||||
Early returns makes the code more linear, reduces conditional/indent depth, and in some cases makes the code faster. In short, it often makes code simpler. The “no early returns” is a soft version of “no gotos”. There are cases where it is not possible to produce good code while following those heuristics. A software engineer should strive to produce the best possible code, not rigidly follow heuristics even when they don’t make sense. There is an element of taste. Don’t create random early returns if it doesn’t improve the code. But there are many, many cases where it makes the code much more readable and maintainable. | |||||||||||||||||||||||||||||
| ▲ | SideburnsOfDoom 2 days ago | parent | prev | next [-] | ||||||||||||||||||||||||||||
The "no early returns" rule came about because was a good rule in context, specifically C and FORTRAN code before roughly 1990. It was part of "structured programming", contemporary to "Go To Statement Considered Harmful", Dijkstra, 1968. And it became received wisdom - i.e. a rule that people follow without close examination. For example of the rule, a function might allocate, do something and then de-allocate again at the end of the block. A second exit point makes it easy to miss that de-allocation, and so introduce memory leaks that only happen sometimes. The code is harder to reason about and the bugs harder to find. source: > A problem with early exit is that cleanup statements might not be executed. ... Cleanup must be done at each return site, which is brittle and can easily result in bugs. https://en.wikipedia.org/wiki/Structured_programming#Early_r... About 90% of us will now be thinking "but that issue doesn't apply to me at all in $ModernLang. We have GC, using (x) {} blocks, try-finally, or we have deterministic finalisation, etc." And they're correct. In most modern languages it's fine. The "no early returns" rule does not apply to Java, TypeScript, C#, Rust, Python, etc. Because these languages specifically made early return habitable. The meta-rule is that some rules persist past the point when they were useful. Understand what a rule is for and then you can say when it applies at all. Rules without reasons make this harder. Some rules have lasted: we typically don't use goto at all any more, just structured wrappers of it such as if-else and foreach | |||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||
| ▲ | mrgaro 2 days ago | parent | prev | next [-] | ||||||||||||||||||||||||||||
I remember having this argument with my professor at the school, who insisted that a function should have only one "return" clause at the very end. Even as I tried, I could not get him to explain why this would be valuable and how does this produce better code, so I'm interested on hearing your take on this? | |||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||
| ▲ | katzenversteher 2 days ago | parent | prev | next [-] | ||||||||||||||||||||||||||||
For me it's mostly about indentation / scope depth. So I prefer to have some early exits with precondition checks at the beginning, these are things I don't have to worry about afterwards and I can start with the rest at indentation level "0". The "real" result is at the end. | |||||||||||||||||||||||||||||
| ▲ | stonemetal12 2 days ago | parent | prev | next [-] | ||||||||||||||||||||||||||||
Because good code checks pre conditions and returns early if they are not met. | |||||||||||||||||||||||||||||
| ▲ | dragonwriter 2 days ago | parent | prev [-] | ||||||||||||||||||||||||||||
> Why is "no early returns" not a good rule? It might be a good guideline. Its not a good rule because slavishly following results in harder to follow code written to adhere to it. | |||||||||||||||||||||||||||||