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.