php-general Digest 29 Jun 2008 21:57:47 -0000 Issue 5541
Topics (messages 276104 through 276116):
Re: Inspiration for a Tombstone.
276104 by: Colin Guthrie
Early return (was: Inspiration for a Tombstone.)
276105 by: Dotan Cohen
276106 by: tedd
276107 by: Dotan Cohen
276108 by: Dotan Cohen
276109 by: Colin Guthrie
276110 by: Colin Guthrie
276111 by: Dotan Cohen
276112 by: Robert Cummings
276113 by: tedd
276114 by: Roberto Costumero Moreno
276115 by: paragasu
276116 by: Colin Guthrie
Administrivia:
To subscribe to the digest, e-mail:
[EMAIL PROTECTED]
To unsubscribe from the digest, e-mail:
[EMAIL PROTECTED]
To post to the list, e-mail:
[EMAIL PROTECTED]
----------------------------------------------------------------------
--- Begin Message ---
Dotan Cohen wrote:
As far as the programming practice that Colin was advocating, it is
not a bad habit. And as far as the computer is concerned, the
efficiency is a wash since the number of internal steps probably
doesn't change much. However, it doesn't always work. (I know - no one
claimed it did.)
<?php
if ($challenge_password_hash = $stored_password_hash) {
echo 'Welcome to the club!';
} else {
echo 'Stay out! This club is for members only!';
}
?>
Andrew
In these instances you could rely on != behaviour instead of ==
behaviour, like this:
<?php
if ($challenge_password_hash != $stored_password_hash) {
echo 'Stay out! This club is for members only!';
} else {
echo 'Welcome to the club!';
}
?>
or, better yet:
<?php
if ($challenge_password_hash != $stored_password_hash) {
echo 'Stay out! This club is for members only!';
exit;
}
echo 'Welcome to the club!';
// Lots of code here that just saved itself another indent in my IDE
?>
Indeed. The technique obviously only works for constants, and it wont
help with variable -> variable comparisons (unless you do something
really stupid like put the first expression in quotes!).
Using != when possible is a good idea but I guess you have to draw the
line as to moving your preferred "flow" of logic around to fit in with a
technique for reducing the possibility of logical errors.
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!!!
Col
--- End Message ---
--- Begin Message ---
2008/6/29 Colin Guthrie <[EMAIL PROTECTED]>:
>> In these instances you could rely on != behaviour instead of ==
>> behaviour, like this:
>>
>> <?php
>> if ($challenge_password_hash != $stored_password_hash) {
>> echo 'Stay out! This club is for members only!';
>> } else {
>> echo 'Welcome to the club!';
>> }
>> ?>
>>
>> or, better yet:
>>
>> <?php
>> if ($challenge_password_hash != $stored_password_hash) {
>> echo 'Stay out! This club is for members only!';
>> exit;
>> }
>> echo 'Welcome to the club!';
>> // Lots of code here that just saved itself another indent in my IDE
>> ?>
>
> Indeed. The technique obviously only works for constants, and it wont help
> with variable -> variable comparisons (unless you do something really stupid
> like put the first expression in quotes!).
>
> Using != when possible is a good idea but I guess you have to draw the line
> as to moving your preferred "flow" of logic around to fit in with a
> technique for reducing the possibility of logical errors.
>
> 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.
Dotan Cohen
http://what-is-what.com
http://gibberish.co.il
א-ב-ג-ד-ה-ו-ז-ח-ט-י-ך-כ-ל-ם-מ-ן-נ-ס-ע-ף-פ-ץ-צ-ק-ר-ש-ת
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
--- End Message ---
--- Begin Message ---
At 3:30 PM +0300 6/29/08, Dotan Cohen wrote:
2008/6/29 Colin Guthrie <[EMAIL PROTECTED]>:
> 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.
Dotan Cohen
Start a new thread.
Cheers,
tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
--- End Message ---
--- Begin Message ---
2008/6/29 Colin Guthrie <[EMAIL PROTECTED]>:
>> In these instances you could rely on != behaviour instead of ==
>> behaviour, like this:
>>
>> <?php
>> if ($challenge_password_hash != $stored_password_hash) {
>> echo 'Stay out! This club is for members only!';
>> } else {
>> echo 'Welcome to the club!';
>> }
>> ?>
>>
>> or, better yet:
>>
>> <?php
>> if ($challenge_password_hash != $stored_password_hash) {
>> echo 'Stay out! This club is for members only!';
>> exit;
>> }
>> echo 'Welcome to the club!';
>> // Lots of code here that just saved itself another indent in my IDE
>> ?>
>
> Indeed. The technique obviously only works for constants, and it wont help
> with variable -> variable comparisons (unless you do something really stupid
> like put the first expression in quotes!).
>
> Using != when possible is a good idea but I guess you have to draw the line
> as to moving your preferred "flow" of logic around to fit in with a
> technique for reducing the possibility of logical errors.
>
> 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.
Dotan Cohen
http://what-is-what.com
http://gibberish.co.il
א-ב-ג-ד-ה-ו-ז-ח-ט-י-ך-כ-ל-ם-מ-ן-נ-ס-ע-ף-פ-ץ-צ-ק-ר-ש-ת
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
--- End Message ---
--- Begin Message ---
2008/6/29 tedd <[EMAIL PROTECTED]>:
> Start a new thread.
>
Done, sorry for the highjack.
Dotan Cohen
http://what-is-what.com
http://gibberish.co.il
א-ב-ג-ד-ה-ו-ז-ח-ט-י-ך-כ-ל-ם-מ-ן-נ-ס-ע-ף-פ-ץ-צ-ק-ר-ש-ת
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
--- End Message ---
--- Begin Message ---
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
--- End Message ---
--- Begin Message ---
Dotan Cohen wrote:
Why not? I do this often, but I am not a professional programmer. I
find this to be very useful.
Found another opinion/article about this. I remember reading this one a
while back:
http://whatimean.wordpress.com/2007/02/08/multiple-return-points-are-bad/
Like I said before, I don't personally subscribe to this point of view,
but it makes for interesting reading :D
Col
--- End Message ---
--- Begin Message ---
2008/6/29 Colin Guthrie <[EMAIL PROTECTED]>:
> Dotan Cohen wrote:
>>
>> Why not? I do this often, but I am not a professional programmer. I
>> find this to be very useful.
>
> Found another opinion/article about this. I remember reading this one a
> while back:
>
> http://whatimean.wordpress.com/2007/02/08/multiple-return-points-are-bad/
>
> Like I said before, I don't personally subscribe to this point of view, but
> it makes for interesting reading :D
>
Thanks. He makes a good point. For my own one-man-show homepage, my
multile-exit strategy is fine. But I do see the value in regard to
code debugging and following the code flow.
Dotan Cohen
http://what-is-what.com
http://gibberish.co.il
א-ב-ג-ד-ה-ו-ז-ח-ט-י-ך-כ-ל-ם-מ-ן-נ-ס-ע-ף-פ-ץ-צ-ק-ר-ש-ת
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
--- End Message ---
--- Begin Message ---
On Sun, 2008-06-29 at 17:47 +0300, Dotan Cohen wrote:
> 2008/6/29 Colin Guthrie <[EMAIL PROTECTED]>:
> > Dotan Cohen wrote:
> >>
> >> Why not? I do this often, but I am not a professional programmer. I
> >> find this to be very useful.
> >
> > Found another opinion/article about this. I remember reading this one a
> > while back:
> >
> > http://whatimean.wordpress.com/2007/02/08/multiple-return-points-are-bad/
> >
> > Like I said before, I don't personally subscribe to this point of view, but
> > it makes for interesting reading :D
> >
>
> Thanks. He makes a good point. For my own one-man-show homepage, my
> multile-exit strategy is fine. But I do see the value in regard to
> code debugging and following the code flow.
I'm a big fan of multiple early returns whenever possible, but once the
code becomes more complex holding back until the end if possible. I find
multiple early returns are very easy to read and then the purpose of the
code doesn't get lost within the convolution of the logic. Similarly, I
use the exact same approach with a loop from which I want to
break/continue. I find it much clearer to see at the very beginning of a
function or loop block exactly what easily fails to meet the criteria
and is discarded early. Additionally, this makes the code more linear in
that far fewer nestings are required. From that I'd argue that the more
linear the code is, the more readable and understandable it is.
Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP
--- End Message ---
--- Begin Message ---
At 3:25 PM +0100 6/29/08, Colin Guthrie wrote:
Dotan Cohen wrote:
Why not? I do this often, but I am not a professional programmer. I
find this to be very useful.
Found another opinion/article about this. I remember reading this
one a while back:
http://whatimean.wordpress.com/2007/02/08/multiple-return-points-are-bad/
Like I said before, I don't personally subscribe to this point of
view, but it makes for interesting reading :D
Whenever possible, I try to keep to a single return in my functions.
For me it usually makes my life easier.
However, there are times that if one insists on keeping to a single
return doctrine, then the code can become very difficult to read
because of additional code to maintain the requirement.
I found an excellent example of this in DOM Scripting (page 99) by
Keith where he takes a sea of curly braces and reduces them down a
few false returns that appear immediately at the beginning of the
function.
So, in this case I clearly support multiple returns provided that
they appear in a logical and obvious location (i.e., in the front of
the function).
Having multiple returns spread throughout a function is not conducive
to providing the reader with easy comprehension as to what the
function is doing (i.e., readability) -- which is the main reason for
all of this concern anyway.
When you can easily understand what your function is doing by
inspection, then you are doing something right.
Cheers,
tedd
PS: Nice try on the starting another thread. :-)
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
--- End Message ---
--- Begin Message ---
There is a point between Multiple & early return vs. One last return, which
is Efficiency.
Imagine you have a code which makes lots of things. What is better in time?
If you have early returns, your script will only do the operations it has
to, nothing more than it has to do.
If you have your data, but still keep calculating some other operations you
don't need for this data (but maybe for another), and then you return, you
are losing precious time of execution.
And making things such as the showed above (for example):
void foo(int bar)
{
int rv;
if (bar)
rv = 1;
else
rv = 0;
return rv;
}
is not that good. Nor even the solution:
function foo($bar)
{
if ($bar)
return true;
return false;
}
Think, that if you have something like $bar (a boolean variable), and if it
is true, you return true, it is best to do this, is better, faster, and if
the expression is simple is legible.
function foo($bar)
{
return $bar
}
Notice that if $bar is true, foo will return true. If $bar is false, foo
will return False. And what if I want to return false if $bar is true and
viceversa?
Simple as this:
function foo($bar)
{
return !$bar;
}
Notice the "!" symbol. Of course, it also is valid for expressions. What if
have a function which returns true if I have a number even and the another
is uneven? Should look like this
function foo($even_number,$uneven_number)
{
return even($even_number) && uneven($uneven_number);
}
And it works and it's simple. Supposing even( ) and uneven( ) functions
return a boolean value y the variable passed is even and uneven.
Cheers
On Sun, Jun 29, 2008 at 17:35, tedd <[EMAIL PROTECTED]> wrote:
> At 3:25 PM +0100 6/29/08, Colin Guthrie wrote:
>
>> Dotan Cohen wrote:
>>
>>> Why not? I do this often, but I am not a professional programmer. I
>>> find this to be very useful.
>>>
>>
>> Found another opinion/article about this. I remember reading this one a
>> while back:
>>
>> http://whatimean.wordpress.com/2007/02/08/multiple-return-points-are-bad/
>>
>> Like I said before, I don't personally subscribe to this point of view,
>> but it makes for interesting reading :D
>>
>
> Whenever possible, I try to keep to a single return in my functions. For me
> it usually makes my life easier.
>
> However, there are times that if one insists on keeping to a single return
> doctrine, then the code can become very difficult to read because of
> additional code to maintain the requirement.
>
> I found an excellent example of this in DOM Scripting (page 99) by Keith
> where he takes a sea of curly braces and reduces them down a few false
> returns that appear immediately at the beginning of the function.
>
> So, in this case I clearly support multiple returns provided that they
> appear in a logical and obvious location (i.e., in the front of the
> function).
>
> Having multiple returns spread throughout a function is not conducive to
> providing the reader with easy comprehension as to what the function is
> doing (i.e., readability) -- which is the main reason for all of this
> concern anyway.
>
> When you can easily understand what your function is doing by inspection,
> then you are doing something right.
>
> Cheers,
>
> tedd
>
> PS: Nice try on the starting another thread. :-)
>
> --
> -------
> 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
>
>
--- End Message ---
--- Begin Message ---
i am a big fan of multiple return.
it save a lot of my time and it makes my code a lot more simple and readable.
On 6/29/08, Roberto Costumero Moreno <[EMAIL PROTECTED]> wrote:
> There is a point between Multiple & early return vs. One last return, which
> is Efficiency.
>
> Imagine you have a code which makes lots of things. What is better in time?
>
> If you have early returns, your script will only do the operations it has
> to, nothing more than it has to do.
>
> If you have your data, but still keep calculating some other operations you
> don't need for this data (but maybe for another), and then you return, you
> are losing precious time of execution.
>
> And making things such as the showed above (for example):
>
> void foo(int bar)
> {
> int rv;
> if (bar)
> rv = 1;
> else
> rv = 0;
> return rv;
> }
>
>
> is not that good. Nor even the solution:
>
> function foo($bar)
> {
> if ($bar)
> return true;
>
> return false;
> }
>
> Think, that if you have something like $bar (a boolean variable), and if it
> is true, you return true, it is best to do this, is better, faster, and if
> the expression is simple is legible.
>
> function foo($bar)
> {
> return $bar
> }
>
> Notice that if $bar is true, foo will return true. If $bar is false, foo
> will return False. And what if I want to return false if $bar is true and
> viceversa?
>
> Simple as this:
>
> function foo($bar)
> {
> return !$bar;
> }
>
> Notice the "!" symbol. Of course, it also is valid for expressions. What if
> have a function which returns true if I have a number even and the another
> is uneven? Should look like this
>
> function foo($even_number,$uneven_number)
> {
> return even($even_number) && uneven($uneven_number);
> }
>
> And it works and it's simple. Supposing even( ) and uneven( ) functions
> return a boolean value y the variable passed is even and uneven.
>
> Cheers
>
>
>
> On Sun, Jun 29, 2008 at 17:35, tedd <[EMAIL PROTECTED]> wrote:
>
>> At 3:25 PM +0100 6/29/08, Colin Guthrie wrote:
>>
>>> Dotan Cohen wrote:
>>>
>>>> Why not? I do this often, but I am not a professional programmer. I
>>>> find this to be very useful.
>>>>
>>>
>>> Found another opinion/article about this. I remember reading this one a
>>> while back:
>>>
>>> http://whatimean.wordpress.com/2007/02/08/multiple-return-points-are-bad/
>>>
>>> Like I said before, I don't personally subscribe to this point of view,
>>> but it makes for interesting reading :D
>>>
>>
>> Whenever possible, I try to keep to a single return in my functions. For
>> me
>> it usually makes my life easier.
>>
>> However, there are times that if one insists on keeping to a single return
>> doctrine, then the code can become very difficult to read because of
>> additional code to maintain the requirement.
>>
>> I found an excellent example of this in DOM Scripting (page 99) by Keith
>> where he takes a sea of curly braces and reduces them down a few false
>> returns that appear immediately at the beginning of the function.
>>
>> So, in this case I clearly support multiple returns provided that they
>> appear in a logical and obvious location (i.e., in the front of the
>> function).
>>
>> Having multiple returns spread throughout a function is not conducive to
>> providing the reader with easy comprehension as to what the function is
>> doing (i.e., readability) -- which is the main reason for all of this
>> concern anyway.
>>
>> When you can easily understand what your function is doing by inspection,
>> then you are doing something right.
>>
>> Cheers,
>>
>> tedd
>>
>> PS: Nice try on the starting another thread. :-)
>>
>> --
>> -------
>> 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
>>
>>
>
--- End Message ---
--- Begin Message ---
Robert Cummings wrote:
I'm a big fan of multiple early returns whenever possible, but once the
code becomes more complex holding back until the end if possible. I find
multiple early returns are very easy to read and then the purpose of the
code doesn't get lost within the convolution of the logic. Similarly, I
use the exact same approach with a loop from which I want to
break/continue. I find it much clearer to see at the very beginning of a
function or loop block exactly what easily fails to meet the criteria
and is discarded early. Additionally, this makes the code more linear in
that far fewer nestings are required. From that I'd argue that the more
linear the code is, the more readable and understandable it is.
I think you've captured very eloquently exactly how I feel about this!
I've often thought that the if you disagree about early return then you
automatically have to hate the whole continue/break thing too when used
in loops - basically language *features*. I do concede (as you do) that
there are times when the code benefits from nesting rather than "early
return/break/continue/exit".
Col
--- End Message ---