Should a function have only one return statement

This is a form of thought that seems to have evolved over time.

I remember when I started programming the de facto standard was one-entry, one-exit. And that is still being taught in many OO courses in universities.

However, there are many schools of thoughts (including more recently mine) that come down on the side that a moderate number of return statements can actually enhance code readability.

For example, compare this:

versus this:

which uses guard clauses [Beck] / [Fowler’s Refactoring].

http://sourcemaking.com/refactoring/replace-nested-conditional-with-guard-clauses

and

http://stackoverflow.com/questions/36707/should-a-function-have-only-one-return-statement/36714#36714

and

http://programmers.stackexchange.com/questions/118703/where-did-the-notion-of-one-return-only-come-from

Leave a Reply

Your email address will not be published. Required fields are marked *