Bill
Bill

Reputation: 130

How to keep only one return statement in a function?

Despite the discussion here, Should a function have only one return statement?, are there some simple tips or method to keep only one return statement? Or how to refactor a multiple return statement to a only one return statement?

Upvotes: 0

Views: 418

Answers (1)

Rahul Tripathi
Rahul Tripathi

Reputation: 172548

First of all it is not clear why you want to do this as in the thread it is already mentioned that this feature was added in modern languages due to some reasons and to allow the user to get more functionality.

The wiki says a lot on it:

Some people make sure each function has a single entry, single exit (SESE). These people argue that The use of a return statement violates structured programming: it is an unstructured exit from the function, resulting in multiple exit points, rather than the single exit point required by structured programming. It has thus been argued[5] that one should eschew the use of the explicit return statement except at the textual end of a subroutine, considering that, when it is used to "return early", it may suffer from the same sort of problems that arise for the GOTO statement. Conversely, it can be argued that using the return statement is worthwhile when the alternative is more convoluted code, such as deeper nesting, harming readability.

Other people say that one or more "guard clauses" -- conditional "early exit" return statements near the beginning of a function -- often make a function easier to read than the alternative.[6][7][8][9]

The most common problem in early exit is that cleanup or final statements are not executed – for example, allocated memory is not unallocated, or open files are not closed, causing leaks. These must be done at each return site, which is brittle and can easily result in bugs. For instance, in later development, a return statement could be overlooked by a developer, and an action which should be performed at the end of a subroutine (e.g., a trace statement) might not be performed in all cases. Languages without a return statement, such as standard Pascal don't have this problem. Some languages, such as C++ and Python, employ concepts which allow actions to be performed automatically upon return (or exception throw) which mitigates some of these issues – these are often known as "try/finally" or similar. Ironically, functionality like these "finally" clauses can be implemented by a goto to the single return point of the subroutine. An alternative solution is to use the normal stack unwinding (variable deallocation) at function exit to unallocate resources, such as via destructors on local variables, or similar mechanisms such as Python's "with" statement.

But even then if you want to achieve the Single entry and Single exit functionality then the best way to do is to get invalid cases out of the way first, either simply exiting or raising exceptions as appropriate, put a blank line in there, then add the "real" body of the method.

Also check Where did the notion of “one return only” come from?

Upvotes: 1

Related Questions