Return early and simplified if else

Lukas posted a very interesting post yesterday, http://pooteeweet.org/blog/0/1107#m1107 on early returns in code which is a subject he and I have talked about couple of times before and I’m still not sure why we haven’t pushed for it into the PEAR standard.

Returning early is a thing I simply love, it can make your code so much more readable with very little effort at all and the flow feels a lot more normal e.g. check for errors and return if found, kinda like airport checks, they check if you are legit at the checkin board and then at the security gate and each time they throw you away if you are not legit.

I’ve been battling with a lot of PEAR code to return early because some of the code can look like a maze, the PEAR installer for example, go see the blog post by Lukas to see couple of examples but here’s a big pet peeve of mine that I try to correct every single time:

 function bar() 
 {
     if ($foo) {
         a lot of fun code         
         return $somethingUseful;     
     } else {
         return false;
     } 
 }

Obviously this is a very minute example but it can be turned into:

 function bar() 
 {     
     if ($foo) {         
         a lot of fun code         
         return $somethingUseful;     
     }     
     
     return false; 
 }

this is what I call shit mixing but still makes it more readable but this way I prefer which is the way Lukas portrays

 function bar() 
 {     
     if (!$foo) {         
         return false;     
     }     

     a lot of fun code     
     return $somethingUseful; 
 }

Think I will try to push this into the PEAR manual one of these days if possible, makes things so much better.

7 Comments to “Return early and simplified if else”

  1. What about:

    function bar()
    {
    $retval = false;
    if ($foo) {
    a lot of fun code
    $retval = $somethingUseful;
    }
    return $retval;
    }

    You’ve got only one return in function. It’s clear.

  2. It’s not simple, you still have that extra indent for what, 90% of your function and that’s IMHO too much clutter

  3. The little make the big better. I agree, go push it into the PEAR, I’m going to edit my codes.

  4. I’ve been using this in my code for quite some time now, and it really is worth it pushing this into PEAR in my humble opinion. More conventions like this should make it easier to interpret code by others.

  5. I totally agree. The sooner you bail, the less code you have to read.

  6. Couldn’t agree more. This is more or less a style standard at Digg since Ian and I have arrived.

  7. I prefer a hybrid like Marcin proposed. One of my CS professors got me hooked on the one-return-point philosophy.

    That said, if you’re going to use multiple return points, I definitely like the method you propose, Helgi.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: