Early Returns Debate
This cluster centers on debates about using early return statements versus nested conditionals or implicit returns for better code readability, guard clauses, and error handling in functions across various programming languages.
Activity Over Time
Top Contributors
Keywords
Sample Comments
The best light I saw this recommendation is: apart from guard statements at the top of a function which have return's, the advice should hold
Why doesn't go enforce using return values (or explicitly swallowing them)?
early return solves so many readability problems. i structure my code in favor of it.
Why shouldn't return be considered a function?
The lack of return statements is rarely missed, although it could be handy in some cases, there are ways around it.There is nothing weird about ifs' behavior once you understand it.Byron's rc tried to 'fix' ifs, and made everything worse.
I prefer to use explicit return statements everywhere -- for greppability.
Haven't looked into it for a while, but, it used to undermine the JIT compiler to use multiple return statements in a single method in Java.That said, I entirely disagree with the premise. I find it vastly easier to reason about a function/method that does not return early. To me, this is on par with writing switch statements that don't cover all the possible values: a sin.
People really like their early return statements however.
Early returns ftw (if (!cond) return; instead of if(cond) do();)!
We can all agree that convoluted logic is bad, but there are functions with early returns which are not convoluted.