"I was always taught" - at some point in life, people begin to learn, not learn :-)
Do not accept anything like the gospel (even from me - if you do not agree, make your own mind). Check out the guidelines to see why they exist.
The reason you were taught that multiple exit points are bad is because they often lead to code that is difficult to execute. In other words, a 400-line function packed with return is difficult to analyze in terms of its behavior.
Your little piece of code does not suffer from this. Next guide: if you see the control flow on one screen in the editor, this is normal. And, since 12 lines will fit into any editor I've used over the past two decades, your code is very readable.
In fact, I saw code from “never use multiple exit points” that is much less readable than the one that will be created by breaking their rules. Usually it includes multi-level while statements, so confusing that they have to break them down into several lines and continue to be painfully parsed.
Aim for readability. If recommendations help with this, use them. If not, throw them out the window.
paxdiablo
source share