Hey #programmers, I'd like to know your point of view about "The single return law". Is it necessary in modern #programingLanguages or is only about old ones? #SoftwareEngineering #Programming #Coding
@codeDude Like so many "laws" it should wind its neck in and admit it's only a guideline. It can be helpful for readability, but ultimately it's readability itself that matters so use however many returns optimizes for it. Personally, though, I find functional languages are more likely to have a single return than imperative ones, regardless of age.
@codeDude when I was much younger, I was expressly told by someone who knows these things that a single return makes it easier to validate safety critical code. I accepted that without a follow-up question, so I don't know why 😕

@codeDude Early in my career, I developed software in Java, which doesn't support multiple returns.

So in postgrad, when I saw Python examples with multiple returns, I clutched my pearls big time. :P

My tutors said that multiple returns are acceptable. I took their word for it, and occasionally use them, but I still prefer to avoid them.

For example, if there's a sprawling function littered with disparate and unrelated exit points, a rethink might be necessary.

@codeDude if you are into mathematically proving algorithms by hand, or if you want to make it easy to add code that runs each time after a function was called, you really benefit from it. It makes it easier to spot edge cases, too. On the other hand the reader has to deal with additional nesting and some people get confused by it.

I personally prefer nesting to multiple exits. But I have to obey the company style guideline.

@codeDude N. Wirth removed the ability to do multiple returns from #OBERON in 2007: “… the unpleasant property that the return statement is syntactically disconnected from the function procedure declaration, similar to the exit from the loop statements. It is therefore difficult to check, whether or not a function procedure declaration specifies a result, or perhaps even several of them.

@codeDude
The "single return" is a relic from languages that require manual resource cleanup. For those, it's a good idea to have a single return and all your cleanup just before that.

If you write code that doesn't need manual cleanup (and you should), feel free to trash that "law". It results in very ugly code IMHO...