As a programmer do you prefer multiple return statements?

Quora Feeds

Active Member
Kurt Guntheroth


The controversy stems from Yourdon’s Structured Programming in the 1960’s. He said every function should have only one return. The idea was to simplify a program’s flowchart and reduce the use of goto’s. Both of these goals were desirable with the languages of the 1960’s.

The real problem with the single-return idea is that in many cases you have to add a state variable to tell if you’re done, so you can get out of the loop, so you can get to the single return statement or to the implicit return at the bottom of the function.

Today, we have languages with structured loops built in, and don’t use gotos. We don’t use flowcharts much. Most languages have explicit return statements. Multiple returns lets you write code that appears simpler.

A common idiom is to do a bunch of input tests and return early on error. Another idiom is to show the logical state with flow-of-control instead of the result of a boolean expression.

I personally am comfortable with multiple returns, and think they make my excellent code stylings more understandable. However I recognize that opinions vary on this subject. With the exception of early error returns, I am comfortable going with the group consensus where I work.



See Questions On Quora

Continue reading...
 
Top