- Prefer a single exit point.
- In void functions, the single exit point should be the physical end of the function.
- In non-void functions, the single exit point should be a return statement at the last line of the function.
- The return statement should be simple and avoid implicit type conversions.
- Avoid multiple return statements.
- Prefer not to throw exceptions.
DiscussionGetting function bodies right is the basis of whatever coding best practices you learn in the future. By writing correct and maintainable function bodies, you’ll have the pieces to join in a solid greater work. This tenth guideline completes a set of recommendations related to writing function bodies.
A good story is one that ends well. The execution of a function body can end in two ways: returning, and throwing an exception. A function returns if the execution flow reaches a return statement, or if it reaches its closing bracket. A function throws if an exception is generated (for example, with a throw statement) and not handled in the function itself. Let’s ignore exceptions by now (most of the time, ignoring them is not as bad an idea as it may seem) and let’s focus on the return.
A function has a return type. Even without having discussed function signatures, you obviously needto know that when you’re writing a function. Let’s divide the return types in two categories: void (i.e. no type) and anything else.
In void function, a return statement forces the function execution to be ended at that point, returning the control flow to the caller code. It’s ok to write return statements, but prefer not using them in this case and instead exiting the function at its end. You will minimize confusions in the human eyes which will read your code (the machine which executes it is never confused).
In non-void functions, prefer a single exit point, in the form of a return statement at the last line of the function. Avoid too complex statements as return statements: prefer returning a constant, a local variable, or the result of a simple query function.
Also, avoid implicit type conversions: don’t end a function which is int with “return 3.0;”. Your floating point literal will implicitly be casted to an integer, but you want to avoid implicit type conversions in your code – you win very little in using them, but you can sometimes lose a lot. An explicit type conversion in a return statement is not much better – if you really need one, prefer isolating it in its single line, prior to the return statement.
[McCONNELL 2004] Code Complete is flexible about multiple return statements, but with a preference for a single return: "Minimize the number of returns in each routine. It's harder to understand a routine when, reading it at the bottom, you're unaware of the possibility that it returned somewhere above. For that reason, use returns judiciously-only when they improve readability.", it says in page 393 as a conclusion of Section 17.1 "Multiple Returns from a Routine", in Chapter 17 "Unusual Control Structures".