Re: [PHP] Multiple return statements in a function.

2009-04-24 Thread Richard Heyes
Hi,

 your function could be condensed to this:

 function check($a)
 {
    return is_array($a) ? true : false;
 }

Or even better, this:

function check($a)
{
   return is_array($a);
}

Not that I'd imagine it makes a great deal of difference.

-- 
Richard Heyes
HTML5 graphing: RGraph (www.rgraph.net)
PHP mail: RMail (www.phpguru.org/rmail)
PHP datagrid: RGrid (www.phpguru.org/rgrid)
PHP Template: RTemplate (www.phpguru.org/rtemplate)

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



Re: [PHP] Multiple return statements in a function.

2009-04-23 Thread Per Jessen
Peter van der Does wrote:

 I tend to put my return value in a variable and at the end of the
 function I have 1 return statement.
 I have seen others doing returns in the middle of the function.
 
 Example how I do it:
 function check($a) {
   $return='';
   if ( is_array( $a ) ) {
 $return='Array';
   } else {
 $return='Not Array';
   }
   return $return;
 }
 
 Example of the other method:
 function check($a) {
 
   if ( is_array( $a ) ) {
 return ('Array');
   } else {
 return ('Not Array');
   }
 }
 
 What is your take? And is there any benefit to either method?

It's only about style and coding logic.  In essence, all the return does
is pop the previous IP off the stack and adjust the stack pointer.  It
doesn't matter where you do that.


/Per
 

-- 
Per Jessen, Zürich (16.2°C)


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



Re: [PHP] Multiple return statements in a function.

2009-04-23 Thread George Larson
On Thu, Apr 23, 2009 at 8:25 AM, Per Jessen p...@computer.org wrote:
 Peter van der Does wrote:

 I tend to put my return value in a variable and at the end of the
 function I have 1 return statement.
 I have seen others doing returns in the middle of the function.

 Example how I do it:
 function check($a) {
   $return='';
   if ( is_array( $a ) ) {
 $return='Array';
   } else {
 $return='Not Array';
   }
   return $return;
 }

 Example of the other method:
 function check($a) {

   if ( is_array( $a ) ) {
 return ('Array');
   } else {
 return ('Not Array');
   }
 }

 What is your take? And is there any benefit to either method?

 It's only about style and coding logic.  In essence, all the return does
 is pop the previous IP off the stack and adjust the stack pointer.  It
 doesn't matter where you do that.


 /Per


 --
 Per Jessen, Zürich (16.2°C)


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



That's an interesting subject that I've never considered.

I usually return immediately.  For me, it makes the code easier to
read.  I work with a number of other coders here and, if the result
isn't returned then I have to keep reading through the code to make
sure nothing else is done with it.  However, when I see the 'return'
then I know we're done there.

That said, I often see questionable coding practices in use at work.
I follow this list (and try to read books about the technologies I
use) because I intend to develop good practices for myself.  That in
mind, if anybody feels strongly about doing it the other way, I'd be
interested in understanding its benefits.

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



Re: [PHP] Multiple return statements in a function.

2009-04-23 Thread Paul M Foster
On Thu, Apr 23, 2009 at 08:12:47AM -0400, Peter van der Does wrote:

 I tend to put my return value in a variable and at the end of the
 function I have 1 return statement.
 I have seen others doing returns in the middle of the function.
 
 Example how I do it:
 function check($a) {
   $return='';
   if ( is_array( $a ) ) {
 $return='Array';
   } else {
 $return='Not Array';
   }
   return $return;
 }
 
 Example of the other method:
 function check($a) {
 
   if ( is_array( $a ) ) {
 return ('Array');
   } else {
 return ('Not Array');
   }
 }
 
 What is your take? And is there any benefit to either method?

As mentioned, this is a matter of style. However, in a lot of cases, in
order to get the return at the very end, it's necessary to put endless
if/else pairs in the function. That makes it hard to read the source.
I'm sure there are purists out there who would insist the single return
go at the end. But expediency and readability may dictate otherwise.

Paul
-- 
Paul M. Foster

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



Re: [PHP] Multiple return statements in a function.

2009-04-23 Thread Tony Marston

tedd tedd.sperl...@gmail.com wrote in message 
news:p06240805c6161613a...@[192.168.1.101]...
 At 8:12 AM -0400 4/23/09, Peter van der Does wrote:
I tend to put my return value in a variable and at the end of the
function I have 1 return statement.
I have seen others doing returns in the middle of the function.

-snip-

What is your take? And is there any benefit to either method?

Peter van der Does

 Peter:

 It's called Structured programming -- one way in and one way out of a 
 function.

 There are, of course, exceptions where it might help others reviewing your 
 code to see what's going on, such as returning a null value if the 
 argument(s) provided are not suitable. But normally the rule is, do not 
 provide an exit from a function in more than one place.

There is no such rule, it is a matter of personal preference. As a 
previous poster has already said, if you want to leave a function early and 
ignore all subsequent processing it is easier to understand if you return 
immediately rather than have a mechanism to jump over the remaining code to 
a single return point. In the good old days we used to use the GOTO in COBOL 
to jump to the exit point, but then people found a way to abuse GOTO in very 
imaginatve ways.

 The benefit is easier to read code.

I think that an immediate return is easier to read, but what do I know - 
I've only been programming for 30 years.

-- 
Tony Marston
http://www.tonymarston.net
http://www.radicore.org

 Cheers,

 tedd

 -- 
 ---
 http://sperling.com  http://ancientstones.com  http://earthstones.com 



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



Re: [PHP] Multiple return statements in a function.

2009-04-23 Thread Per Jessen
George Larson wrote:

 That's an interesting subject that I've never considered.
 
 I usually return immediately.  For me, it makes the code easier to
 read.  I work with a number of other coders here and, if the result
 isn't returned then I have to keep reading through the code to make
 sure nothing else is done with it.  However, when I see the 'return'
 then I know we're done there.

I tend to try to have just one return point, but I will occasionally
have more, typically when a function returns more than just true or
false.  
If it's a true or false outcome, but I still have multiple failure
points, I'll sometimes use a construct like this:


rc=0;
while(true){

  if ( cond1 ) { errormsg; rc=1; break; }
  if ( cond2 ) { errormsg; rc=1; break; }
  if ( cond3 ) { errormsg; rc=1; break; }
  if ( cond4 ) { errormsg; rc=1; break; }
  if ( cond5 ) { errormsg; rc=1; break; }
  if ( cond6 ) { errormsg; rc=1; break; }
  if ( cond7 ) { errormsg; rc=1; break; }

  break;
}

return rc;



-- 
Per Jessen, Zürich (16.6°C)


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



Re: [PHP] Multiple return statements in a function.

2009-04-23 Thread tedd

At 8:12 AM -0400 4/23/09, Peter van der Does wrote:

I tend to put my return value in a variable and at the end of the
function I have 1 return statement.
I have seen others doing returns in the middle of the function.

-snip-

What is your take? And is there any benefit to either method?

Peter van der Does


Peter:

It's called Structured programming -- one way in and one way out of 
a function.


There are, of course, exceptions where it might help others reviewing 
your code to see what's going on, such as returning a null value if 
the argument(s) provided are not suitable. But normally the rule is, 
do not provide an exit from a function in more than one place.


The benefit is easier to read code.

Cheers,

tedd

--
---
http://sperling.com  http://ancientstones.com  http://earthstones.com

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



Re: [PHP] Multiple return statements in a function.

2009-04-23 Thread Richard Heyes
Hi,

 while(true){

Yikes.

Personally, I'd put the return value wherever it will make the code
easier to read. If you're checking what has been passed as arguments,
and one of them is wrong, I think there's little point in continuing,
so an immediate return is the order of the day. Though with
exceptions, this could be mitigated (IIRC). BTW there's also something
to be said for code conciseness, which I think is loosely related. Eg
your function could be condensed to this:

function check($a)
{
return is_array($a) ? true : false;
}

But then the question is nullified somewhat.

-- 
Richard Heyes
HTML5 graphing: RGraph (www.rgraph.net)
PHP mail: RMail (www.phpguru.org/rmail)
PHP datagrid: RGrid (www.phpguru.org/rgrid)
PHP Template: RTemplate (www.phpguru.org/rtemplate)

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



Re: [PHP] Multiple return statements in a function.

2009-04-23 Thread Richard Quadling
2009/4/23 Tony Marston t...@marston-home.demon.co.uk:

 tedd tedd.sperl...@gmail.com wrote in message
 news:p06240805c6161613a...@[192.168.1.101]...
 At 8:12 AM -0400 4/23/09, Peter van der Does wrote:
I tend to put my return value in a variable and at the end of the
function I have 1 return statement.
I have seen others doing returns in the middle of the function.

-snip-

What is your take? And is there any benefit to either method?

Peter van der Does

 Peter:

 It's called Structured programming -- one way in and one way out of a
 function.

 There are, of course, exceptions where it might help others reviewing your
 code to see what's going on, such as returning a null value if the
 argument(s) provided are not suitable. But normally the rule is, do not
 provide an exit from a function in more than one place.

 There is no such rule, it is a matter of personal preference. As a
 previous poster has already said, if you want to leave a function early and
 ignore all subsequent processing it is easier to understand if you return
 immediately rather than have a mechanism to jump over the remaining code to
 a single return point. In the good old days we used to use the GOTO in COBOL
 to jump to the exit point, but then people found a way to abuse GOTO in very
 imaginatve ways.

 The benefit is easier to read code.

 I think that an immediate return is easier to read, but what do I know -
 I've only been programming for 30 years.

 --
 Tony Marston
 http://www.tonymarston.net
 http://www.radicore.org

 Cheers,

 tedd

 --
 ---
 http://sperling.com  http://ancientstones.com  http://earthstones.com



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



As someone's who been playing this game for only 29 years, I like a
single exit point.

One of the issues I found with multiple exit points is that you have
to work a little harder to guarantee a return.

I always write my code so that if the function did nothing, then null
or false are the return values ...

function foo() {
 $result = False;
// ... code which may or may nor affect $result
 return $result;
}

If your code is heavily nested (maybe an indicator that some
refactoring is worth undertaking) and you introduce yet more code,
keeping track of multiple exit points can be a little problematic.

I suppose if you are the only developer then create a style and stick with it.

Don't be afraid of refactoring your code if you feel that a different
style is easier to work with.

If you work in a team, then once the team has determined its style,
stick with it. Even if it hurts. The hassle you'll get when your code
doesn't fit is far more than the learning curve you'll go through
getting the style.

Richard.


-- 
-
Richard Quadling
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498r=213474731
Standing on the shoulders of some very clever giants!

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



Re: [PHP] Multiple return statements in a function.

2009-04-23 Thread Per Jessen
Richard Heyes wrote:

 Hi,
 
 while(true){
 
 Yikes.
 
 Personally, I'd put the return value wherever it will make the code
 easier to read. If you're checking what has been passed as arguments,
 and one of them is wrong, I think there's little point in continuing,
 so an immediate return is the order of the day.

Well, I usually use that construct where I can have a number of
different error-conditions - e.g. if I have a sequence of socket
create, DNS lookup, bind, connect  etc. 


/Per

-- 
Per Jessen, Zürich (15.6°C)


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



Re: [PHP] Multiple return statements in a function.

2009-04-23 Thread tedd

At 2:19 PM +0100 4/23/09, Tony Marston wrote:

tedd tedd.sperl...@gmail.com wrote in message
  It's called Structured programming -- one way in and one way out of a

 function.

 There are, of course, exceptions where it might help others reviewing your
 code to see what's going on, such as returning a null value if the
 argument(s) provided are not suitable. But normally the rule is, do not
 provide an exit from a function in more than one place.


There is no such rule, it is a matter of personal preference. As a
previous poster has already said, if you want to leave a function early and
ignore all subsequent processing it is easier to understand if you return
immediately rather than have a mechanism to jump over the remaining code to
a single return point. In the good old days we used to use the GOTO in COBOL
to jump to the exit point, but then people found a way to abuse GOTO in very
imaginatve ways.


 The benefit is easier to read code.


I think that an immediate return is easier to read, but what do I know -
I've only been programming for 30 years.

--
Tony Marston


Tony:

Don't get your panties in a knot. :-)

I have 44 years of programming under my belt, so what? However, I 
wish I could remember everything I learned during that time.


But what I do remember is there's a school of thought called 
Structured Programming that has a doctrine in which every function 
should have only one entry and exit point.


Now maybe you want to argue with that concept, that's fine -- but the 
point remains this is a rule under Structured Programming. That's 
history.


Now, I usually follow that rule for I have learned from experience 
that in most cases, it is easier to read what is going on in a 
function if you only have one exit.


As I said in my post, there are of course exceptions. There are times 
that requiring a function to have a single exit point will 
unnecessarily create code that's hard to read -- so doing it 
differently is something to consider in those cases. If you want to 
have an immediate exit point in your functions and that makes your 
code more readable, then that's fine and I'm not arguing that point.


However, I am saying (after years of reading other people's code) it 
is generally much easier to read code that follows Structured 
Programming than it is to read code that doesn't.


Cheers,

tedd


--
---
http://sperling.com  http://ancientstones.com  http://earthstones.com

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



Re: [PHP] Multiple return statements in a function.

2009-04-23 Thread Robert Cummings
On Thu, 2009-04-23 at 10:14 -0400, tedd wrote:
 At 2:19 PM +0100 4/23/09, Tony Marston wrote:
 tedd tedd.sperl...@gmail.com wrote in message
It's called Structured programming -- one way in and one way out of a
   function.
 
   There are, of course, exceptions where it might help others reviewing your
   code to see what's going on, such as returning a null value if the
   argument(s) provided are not suitable. But normally the rule is, do not
   provide an exit from a function in more than one place.
 
 There is no such rule, it is a matter of personal preference. As a
 previous poster has already said, if you want to leave a function early and
 ignore all subsequent processing it is easier to understand if you return
 immediately rather than have a mechanism to jump over the remaining code to
 a single return point. In the good old days we used to use the GOTO in COBOL
 to jump to the exit point, but then people found a way to abuse GOTO in very
 imaginatve ways.
 
   The benefit is easier to read code.
 
 I think that an immediate return is easier to read, but what do I know -
 I've only been programming for 30 years.
 
 --
 Tony Marston
 
 Tony:
 
 Don't get your panties in a knot. :-)
 
 I have 44 years of programming under my belt, so what? However, I 
 wish I could remember everything I learned during that time.
 
 But what I do remember is there's a school of thought called 
 Structured Programming that has a doctrine in which every function 
 should have only one entry and exit point.
 
 Now maybe you want to argue with that concept, that's fine -- but the 
 point remains this is a rule under Structured Programming. That's 
 history.
 
 Now, I usually follow that rule for I have learned from experience 
 that in most cases, it is easier to read what is going on in a 
 function if you only have one exit.
 
 As I said in my post, there are of course exceptions. There are times 
 that requiring a function to have a single exit point will 
 unnecessarily create code that's hard to read -- so doing it 
 differently is something to consider in those cases. If you want to 
 have an immediate exit point in your functions and that makes your 
 code more readable, then that's fine and I'm not arguing that point.
 
 However, I am saying (after years of reading other people's code) it 
 is generally much easier to read code that follows Structured 
 Programming than it is to read code that doesn't.

Actually I use an early return as much as possible also. If there are 5
conditions which disqualify the rest of the function from running then
they'll all be listed one after the other at the top of the function.
This way it's very easy to see exactly what doesn't qualify for
evaluation. Additionally, it saves on the need for that many levels of
indentation or making an overly complex conditional. I find it much more
intuitive than mentally tracking several levels of indentation and
scrolling to the bottom of the function.

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


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



Re: [PHP] Multiple return statements in a function.

2009-04-23 Thread Paul M Foster
On Thu, Apr 23, 2009 at 10:14:28AM -0400, tedd wrote:

 At 2:19 PM +0100 4/23/09, Tony Marston wrote:

snip

 There is no such rule, it is a matter of personal preference. As a
 previous poster has already said, if you want to leave a function early 
 and
 ignore all subsequent processing it is easier to understand if you return
 immediately rather than have a mechanism to jump over the remaining code 
 to
 a single return point. In the good old days we used to use the GOTO in 
 COBOL
 to jump to the exit point, but then people found a way to abuse GOTO in 
 very
 imaginatve ways.

  The benefit is easier to read code.

 I think that an immediate return is easier to read, but what do I know -
 I've only been programming for 30 years.

 --
 Tony Marston

 Tony:

 Don't get your panties in a knot. :-)

I think in Tony's case, it would knickers in a twist. ;-}

Paul

-- 
Paul M. Foster

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



Re: [PHP] Multiple return statements in a function.

2009-04-23 Thread tedd

At 10:25 AM -0400 4/23/09, Robert Cummings wrote:

On Thu, 2009-04-23 at 10:14 -0400, tedd wrote:
  However, I am saying (after years of reading other people's code) it

 is generally much easier to read code that follows Structured
 Programming than it is to read code that doesn't.


Actually I use an early return as much as possible also. If there are 5
conditions which disqualify the rest of the function from running then
they'll all be listed one after the other at the top of the function.
This way it's very easy to see exactly what doesn't qualify for
evaluation. Additionally, it saves on the need for that many levels of
indentation or making an overly complex conditional. I find it much more
intuitive than mentally tracking several levels of indentation and
scrolling to the bottom of the function.

Cheers,
Rob.


Rob:

Again, I'm not arguing the point -- we actually agree.

I have functions where I do that as well. My only criteria is how 
easy my functions are to read and understand.


If by placing function entry-criteria at the beginning of the 
function and then returning nulls (or whatever) makes it easier for 
someone to review your code and understand what it does, then by all 
means do it.


But I am sure that both of you have reviewed functions that have 
multiple returns spread throughout where you thought What the hell 
is this? Just what is this function returning? That is what I am 
addressing.


The OP asked which was the better practice and I replied that it is 
USUALLY good practice to have only one return and have it at the end 
of your function -- I stand by that.


Cheers,

tedd


--
---
http://sperling.com  http://ancientstones.com  http://earthstones.com

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