Return: get out as soon as possible

I believe the idea of a single exit point for functions comes from the 70's. A function used to have only one exit point, a single return statement. I still see some developers doing this but it's not a good idea. As soon as you have found what you were looking for, just get out the function.

The old style:

  1.  
  2. public int someMethod(String input) {
  3. int code = 0;
  4. if (someCondition(input))
  5. code = 1;
  6. else if (someOtherCondition(input))
  7. code = 2
  8. return code;
  9. }
  10.  

Improved: returning the value as soon as it's found:

  1.  
  2. public int someMethod(String input) {
  3. if (someCondition(input))
  4. return 1;
  5. if (someOtherCondition(input))
  6. return 2;
  7. return 0;
  8. }
  9.  

The second example is not only shorter but also less exposed to defects. In the first example, if for some reason I forgot the "else" and the second condition applies, I'd be returning the wrong value.

The reasons to return as soon as possible are:

  • Code is usually more simple
  • The likelihood of future defects is lower
Enjoyed reading this post?
Subscribe to the RSS feed and have all new posts delivered straight to you.
  • Iván Stepaniuk

    More than one ‘return’ makes code difficult to read when you have methods with dozens of lines. If we stick to short five line methods we can return at many points without compromising legibility 🙂

  • Ronny Springer

    It’s not an improvement for JS to drop the type definition of variable at the first line. I prefer the first example because it’s better to read (more like a book) and it’s more similar to tests (arrange/act/assert = arrange/act/return).

  • http://andypalmer.com Andy Palmer

    A single point of return is useful in an unmanaged memory language (such as C variants). It means that you can dispose of malloc’d variables that are about to go out of scope.

    That’s just one of the reasons I prefer managed memory languages 😉

  • carlosble

    Makes sense! I believe it was my C programming language teacher, back in colleague the one who told me to have only one return. Thanks 🙂

  • carlosble

    Paulino Corujo on Twitter pointed out this nice example by Fowler: http://martinfowler.com/refactoring/catalog/replaceNestedConditionalWithGuardClauses.html

  • carlosble

    Really? Have a look at this example by Fowler: http://martinfowler.com/refactoring/catalog/replaceNestedConditionalWithGuardClauses.html
    Thanks 🙂

  • carlosble

    Make sense! fortunately we are now more used to short methods than big horrible messes 😉 Thanks man

  • knocte

    Also: there are less assignments (less mutability).

  • cesar

    But one exit point facilitates debugging.

  • http://www.carlosble.com/ Carlos Ble

    Why? Can you provide an example? Thanks

  • http://www.carlosble.com/ Carlos Ble

    Long time non see Andres! Nice to see you again 🙂

  • cesar

    You cannot place just a breakpoint or a traze in the function in order to check the returned value.
    Furthemore the not usage of brackets difficult the introduction of trazes if you wish and it is error prone.

  • http://www.carlosble.com/ Carlos Ble

    Still don’t see your point but I guess it depends on the programming language. I usually don’t have to debug much so maybe this is why I don’t get the point. Thanks 🙂

  • Guest

    Sorry but I find Martin’s example to be horrific.

    double getPayAmount() {
    if (_isDead) return deadAmount();
    if (_isSeparated)
    doSomeMagic();

    return separatedAmount();

    if (_isRetired) return retiredAmount();

    return normalPayAmount();

    };