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].



Leave a Reply

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