4

Possible Duplicate:
Why should a function have only one exit-point?

As a CS student I have had it beaten into my head that there should only be one exit point in a function, at the end.

eg. This:

function foo()
{

    //do stuff here

    if($bar)
    {
         $out = FALSE;
    }
    else
    {
         $out = TRUE;
    }
    return $out;
}

Not:

function foo()
{

    //do stuff here

    if($bar)
    {
         return FALSE;
    }
    return TRUE;
}

However I have seen this second type of exiting used quite often in other peoples code in php, and even in core code for some frameworks (like Kohana which I have been using lately). Is this method of exiting a function considered okay in php standards?

Edit: I can see why I have been told not to do it as it can be easier to track some problems in a function with one exit point, other times I can see why it should be allowed as other problems are better solved or tracked in functions with multiple exit points.

Edit 2: Added "do stuff here" comments to the code example to make people happy

Community
  • 1
  • 1
asdf
  • 357
  • 2
  • 11
  • 10
    Screw one and only one exit point. Almost everyone agrees it should be a case by case descision, where code clarity decides what you do. – goat Jun 04 '12 at 16:48
  • 3
    You will find that PHP isn't very standardised at all. Not even the standard library of functions will take parameters in any coherent order, and there's several different naming schemes for functions. Also, voting for closing because this question cannot be objectively answered. – Emil Vikström Jun 04 '12 at 16:48
  • $bar would always be false as its not ever set but your returning true.. pseudo or not its wrong... – Lawrence Cherone Jun 04 '12 at 16:50
  • 1
    Seems like more of a preference question for me. If calculations are no longer needed and you have the final return value, why not `return` it already? It'll have the same outcome in the end. You should do what you consider easier to read and maintain. – Fabrício Matté Jun 04 '12 at 16:50
  • 3
    I use many return points, at least to maintain readability and to avoid unnecessary brackets. It allows you to write less. I use it in CS, PHP, etc. – Rolice Jun 04 '12 at 16:50
  • 2
    The rule does not make sense in PHP as it's a managed language. There is no manual cleanup needed before exiting a function, so you can just return wherever. – Esailija Jun 04 '12 at 16:55

3 Answers3

2

I've always used the latter route, since you would have to declare $out and have one more variable in existence. But in retrospect, it's just a boolean -- it's not doing any harm. The first route could look cleaner, depending on the context of your code.

It all comes down to consistency. As long as you have a system, determining when it is time to use route 1 or route 2, you're doing great.

Litty
  • 1,856
  • 1
  • 16
  • 35
2

It's six of one, or half a dozen of another - happiness lies in consistency.

GDP
  • 8,109
  • 6
  • 45
  • 82
2

I've seen (good) code return values both ways. If you're using a framework/codebase that consistently uses one way, I would follow that. Otherwise, use what you're comfortable using. :D

Dale Smith
  • 305
  • 2
  • 7