Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should a function have only one exit-point? [closed]

Tags:

coding-style

I've always heard about a single exit-point function as a bad way to code because you lose readability and efficiency. I've never heard anybody argue the other side.

I thought this had something to do with CS but this question was shot down at cstheory stackexchange.

like image 246
hex bob-omb Avatar asked Jan 29 '11 19:01

hex bob-omb


People also ask

Why is single entry single exit important?

Advantages: normal code flow (i.e. non-error) flows cleanly through the top of the code. no chance of failing one "something" and passing another "something" inadvertently executing a block of code. you can enforce scope on code blocks; including clean-up on exit of scope for things used in sub-blocks of code.

Why should a function or method have just one return?

It is sometimes said that a method should have only one return statement (i.e. one exit point) and that to code using more than one return per method is bad practice. It is claimed to be a risk to readability or a source of error. Sometimes this is even given the title of the “single exit point law”.

Can a loop have multiple exit points?

The multiple exit loop construct is a control struc- ture which appears frequently in programming problems and which otherwise can only be realized using state variables and/or goto statements. Using this construct clarifies the program structure, and allows easy correct- ness proofs as well as code optimization.

How many returns should a function have?

"Some programmers follow Edsger Dijkstra's rules of structured programming. Dijkstra said that every function, and every block within a function, should have one entry and one exit. Following these rules means there should only be one return statement in a function, no break or continue statements in a loop…


1 Answers

There are different schools of thought, and it largely comes down to personal preference.

One is that it is less confusing if there is only a single exit point - you have a single path through the method and you know where to look for the exit. On the minus side if you use indentation to represent nesting, your code ends up massively indented to the right, and it becomes very difficult to follow all the nested scopes.

Another is that you can check preconditions and exit early at the start of a method, so that you know in the body of the method that certain conditions are true, without the entire body of the method being indented 5 miles off to the right. This usually minimises the number of scopes you have to worry about, which makes code much easier to follow.

A third is that you can exit anywhere you please. This used to be more confusing in the old days, but now that we have syntax-colouring editors and compilers that detect unreachable code, it's a lot easier to deal with.

I'm squarely in the middle camp. Enforcing a single exit point is a pointless or even counterproductive restriction IMHO, while exiting at random all over a method can sometimes lead to messy difficult to follow logic, where it becomes difficult to see if a given bit of code will or won't be executed. But "gating" your method makes it possible to significantly simplify the body of the method.

like image 92
Jason Williams Avatar answered Sep 22 '22 05:09

Jason Williams