Should a function have only one return statement?

I would say that you can definitely have more than one return statement. Therefore I would like to suggest to:

1.) Minimize the number of returns in each routine. It’s harder to understand a routine if, reading it at the bottom, you’re unaware of the possibility that it returned somewhere above.

2.)Use a return when it enhances readability. In certain routines, once you know the answer, you want to return it to the calling routine immediately. If the routine is defined in such a way that it doesn’t require any cleanup, not returning immediately means that you have to write more code.

Question: Which of the functions shown below would you prefer and why?

Version 1:

function getTemplateId(array $config, $id)
{
    if (array_key_exists($id, $config)) {
        $templateId = $config[$id];
    } else {
        $templateId = 0;
    }

    return $templateId;
}

Version 2:

function getTemplateId(array $config, $id)
{
    if (array_key_exists($id, $config)) {
        return $config[$id];
    }

    return 0;
}

Please write your answer as a comment :-)

9 thoughts on “Should a function have only one return statement?

  1. I would surely go for version 2. It makes super easy to see in which conditions is the template id 0. If this is what I’m looking for then it takes 1 second to find out.
    If I’m looking for results != 0 then I have to focus ONLY on the code in the brackets and not on the rest which also reduces the effort to find something.

    Very good subject, would really like to see some real arguments and not „I like it because it is nicer“ or „we have always did it this way“ (facepalm) or „it stays ion our guidelines, I have to do it this way“ (double facepalm, implements the first facepalm)

  2. Hi Gjero,
    i think the most important part schould be:
    * Is the functionality easy to understand?
    * Is it clear what it does.
    * Is it easy to use?

    When not there are some rules that can make a function / method more readable eg. your rules from above. Sometimes it is more important to use clear and easy to understand function / variable names.

    I prefer version 2, because it is easy to understand for me. No extra code / syntax.

    $templates= array(
      0 => "test first", 
      1 => "test",  
      2 => "another test",
      3 => "test here"
    );
             
    function getTemplateById($id, array $templateCollection)
    {
        if (array_key_exists($id, $templateCollection)) {
            return $templateCollection[$id];
        }
    
        return false;
    }
    
    if ( $template = getTemplateById(10, $templates) ){
      print('template: ' . $template);
    } else {
      print('no template found!');
    }
    

    Another Version could be:
    The benefit of this version is, it is clear what it returns. So its a mix of 1 and 2.

    function getTemplateById($id, array $templateCollection)
    {
        $templateId = false;
    
        if (array_key_exists($id, $templateCollection)) {
            $templateId = $templateCollection[$id];
        }
    
        return $templateId;
    }
    
  3. My vote definitly goes to have only one return statement. The example function would then be:

    function getTemplateId(array $config, $id)  
    {  
    	$templateId = null;
    
        if (array_key_exists($id, $config)) {  
            $templateId = $config[$id];  
        } 
      
        return $templateId;  
    }
    

    In case the function is to hard to read with the bottom return statement, the function itself is may to big or has as to high complexity. :-)

  4. I think both examples are good to read. If you follow good programming practices, this question does not matter. (For example if you manage to have small functions like this, the code will always be good readable. But if you write larger functions, it could be good better, if you have only one return statement, but you did not follow good programming practices ;)

  5. In this case it is definitely version 2. It’s all about readability and for me returning makes it obvious for the person who reads it, that in this case the decision has been made and the value should not be manipulated later on. This prevents bugs in the future …

    “The ratio of time spent reading (code) versus writing is well over 10 to 1 … (therefore) making it easy to read makes it easier to write.” Robert C. Martin (Uncle Bob)

  6. A quite interesting topic! As we can see, there are six votes. Five of the votes prefer the clearest and shortest version, and only one vote, which has no well-founded
    reasons, is for the first version. Anyway, we could say that „version 2“ is the winner :-)

    p.s. my favorite comment is Andre’s

    Your,
    Uncle Bob

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.