Dotan Cohen wrote:
Even your example above hints at another "political" minefield - Early
Return or Multiple Return Points.... (s/Return/Exit/ in this case) Let's not
even go there!!!


Why not? I do this often, but I am not a professional programmer. I
find this to be very useful.

I think this was discussed on this list a while back... or perhaps it was just in an article I read...

/me googles....

I think it stemmed from this article:
http://www.theregister.co.uk/2007/12/04/multiple_exit_wounds/

See the comments to see how different folks agree/disagree.

Personally I do use early return, and use it whenever possible. I find that deep nesting is actually harder to read and in many cases do stuff like:

function foo($bar)
{
  if ($bar)
   return true;

  return false;
}


Rather than:
function foo($bar)
{
  if ($bar)
   $rv = true;
  else
   $rv = false;
  return $rv;
}


If I did need to use a variable for a more complex function I almost always do:

function foo($bar)
{
  $rv = false;
  if ($bar)
   $rv = true;
  return $rv;
}

e.g I *always* "define" the variable at the scope in which I use it. Call me old fashioned but if I generally feel that in the example before last, $rv should not be defined outside of the if/else statement as it was not declared... I know PHP is very loose here, but from a C++ perspective:

void foo(int bar)
{
  if (bar)
  {
    int rv;
    rv = 1;
  }
  else
  {
    int rv;
    rv = 0;
  }
  return rv;
}

This clearly bombs.     

So you generally do:
void foo(int bar)
{
  int rv;
  if (bar)
    rv = 1;
  else
    rv = 0;
  return rv;
}

But then you could also do:
void foo(int bar)
{
  int rv = 0;
  if (bar)
    rv = 1;
  return rv;
}


And so this is how I generally structure my code in PHP too.... call me paranoid/stuck in my ways if you like :)

I appreciate I've steered this away from early return :)

Col


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to