« A fond farewell to Purple Scout | Main | Varför vaccinerade jag mig? »
Wednesday
Nov182009

My opinions on multiple function / method exit points

I originally posted this at Stack Overflow as a response to this question. Since then some people have asked me the same questions outside, so I figured I should just post it here.

There are good things to say about having a single exit-point, just as there are bad things to say about the inevitable "arrow" programming that results.

If using multiple exit points during input validation or resource allocation, I try to put all the 'error-exits' very visibly at the top of the function.

Both the Spartan Programming article of the "SSDSLPedia" and the single function exit point article of the "Portland Pattern Repository's Wiki" have some insightful arguments around this.

If you really want a single exit-point (in any non-exception-enabled language) for example in order to release resources in one single place, I find the careful application of goto to be good; see for example this rather contrived example (compressed to save screen real-estate):

int f(int y) {
    int value = -1;
    void *data = NULL;

    if (y < 0)
        goto clean;

    if ((data = malloc(123)) == NULL)
        goto clean;

    /* More code */

    value = 1;
clean:
   free(data);
   return value;
}

Personally I, in general, dislike arrow programming more than I dislike multiple exit-points, although both are useful when applied correctly. The best, of course, is to structure your program to require neither. Breaking down your function into multiple chunks usually help :)

Although when doing so, I find I end up with multiple exit points anyway as in this example, where some larger function has been broken down into several smaller functions:

int g(int y) {
  value = 0;

  if ((value = g0(y, value)) == -1)
    return -1;

  if ((value = g1(y, value)) == -1)
    return -1;

  return g2(y, value);
}

Depending on the project or coding guidelines, most of the boiler-plate code could be replaced by macros. As a side note, breaking it down this way makes the functions g0, g1 ,g2 very easy to test individually.

Obviously, in an OO and exception-enabled language, I wouldn't use if-statements like that (or at all, if I could get away with it with little enough effort), and the code would be much more plain. And non-arrowy. And most of the non-final returns would probably be exceptions.

In short;

  • Few returns are better than many returns
  • More than one return is better than huge arrows, and guard clauses are generally ok.
  • Exceptions could/should probably replace most 'guard clauses' when possible.

PrintView Printer Friendly Version

EmailEmail Article to Friend

References (108)

References allow you to track sources for this article, as well as articles that were written in response to this article.
  • Response
    usahaku terbaikku saat ini
  • Response
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: arthur falcone
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: arthur falcone
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Arthur Falcone
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: arthur falcone
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: arthur falcone
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: .VBidwEunDfM
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Arthur Falcone
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: UK Models
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Imperial Advance
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: UK Models
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: UK Models
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Corey Park scam
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Corey Park scam
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Corey Park scam
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Corey Park scam
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Red Payments
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Corey Park scam
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Corey Park scam
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Corey Park scam
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Corey Park scam
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Corey Park scam
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: UK Models
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Corey Park scam
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Corey Park scam
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Corey Park scam
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Corey Park scam
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Corey Park scam
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Imperial Advance
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Corey Park scam
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Corey Park scam
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Corey Park scam
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Imperial Advance
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Kion Kashefi
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Corey Park scam
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Corey Park scam
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Imperial Advance
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Corey Park scam
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Corey Park scam
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Corey Park scam
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Corey Park scam
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Corey Park scam
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Corey Park scam
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Corey Park scam
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Corey Park scam
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Imperial Advance
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Imperial Advance
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Imperial Advance
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Corey Park scam
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Corey Park scam
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Dr. Rashmi Patel
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Createurs de Luxe
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: UK Models
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Nicholas Alsis
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Createurs de Luxe
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: UK Models
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: OneTwoTrade
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Vince Malfitano
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Belinda Broido
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Corey Park
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: groupwise inc
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Vincent Malfitano
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Phil St Ores
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Corey Park scam
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Corey Park scam
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Corey Park scam
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Jeff Halevy
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Jeff Halevy
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: phil st ores
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Jeff Halevy
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Jeff Halevy
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: UK Models
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Corey Park arrest
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Vincent Malfitano
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: UK Models
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Link Schwartz
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Link K Schwartz
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Corey Park scam
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Kion Kashefi
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Brandon Colker
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Brandon Colker
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Brandon Colker
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Imperial Advance
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Imperial Advance
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Imperial Advance
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Imperial Advance
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: brian poe
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Brian Poe
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Brian Poe
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Brian Poe
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Eric Gonchar
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Eric Gonchar
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: P Andrew Fleming
    Fnordology - Journal - My opinions on multiple function / method exit points
  • Response
    Response: Sign up

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>