Few pastimes are more pointless and unproductive than arguing about coding style. As long as the basic requirement of consistency is present, there is really next to nothing that wouldn’t fly. You may hear stories of certain… unusual approaches, like using 3-space indentation to “offend all equally”, but it still doesn’t give you the license to criticize – or worse yet, disobey. If you chose to contribute to a particular codebase, you follow the standard, period.
With this out of the way, I will go ahead to pick on my personal pet peeve by pointing out that the rule of single return from function is just dumb!… To my excuse, however, it is not really a stylistic rule, which is also one of the reasons I consider it inane. Since it changes the algorithmic structure of a function, it’s actually about semantics and should be treated as architectural guideline.
Even then, what’s wrong with avoiding more than one return
statement? A couple of things come to mind.
There is a particular “idiom” that you may encounter from time to time, especially in beginner’s code, or just code written by someone who had a particularly bad day. Here’s how it goes:
While it looks completely silly, its only felony is carrying the result through one more place than it’s absolutely necessary. But we would never write such a thing with clear mind. We would point it out immediately in code review. We would fix it whenever encountered.
And yet, it’s essentially the same pattern as in the code below:
It has just one redundant layer of complexity: the success
variable. We could remove it and replace the assignments to it by direct return true;
and return false;
. But that, of course, would violate the rule of single return point, and therefore code meant to follow it is stuck with flaws equivalent to having no idea how basic control statements and boolean logic works.
If we are happily condemning the use of more than one return
statement, we could as well stay completely true to our beliefs. Why not prohibit all of such statements, and in fact use a language that just plain doesn’t support them?…
I’m totally baffled by any claims purporting that this technique makes code more readable. Not only the return value is now semi-hidden, disguised as one of the ordinary variable assignments, but also the return point itself is completely lost. An execution path may end inside an if
inside while
inside a switch
, somewhere deep in the function innards, and you wouldn’t even notice without tracing all the closing braces end;
s, or following the changes in indentation levels.
I’m all for disincentivizing long functions but really, there are much better ways to do that.
Although by far the most braindead, the rule of single return is unfortunately not the only one of its kind. There is a whole class of misguided principles that apply to certain flow control instructions: return
, break
and continue
. What they share in common is the great potential of obfuscating the real meaning of some parts of the algorithms, simply because of forcing the code to be structured in a specific, rigid way.
Why it’s bad? Because one size does not fit all. Abusing early returns is hardly commendable:
but avoiding them at all costs won’t make the code better by any stretch:
Since every function is different, trying to mold them into some over-specified template will just end awry.