NYCPHP Meetup

NYPHP.org

[nycphp-talk] Question about explicit returns

Ben Sgro ben at projectskyline.com
Fri Jan 11 13:37:51 EST 2008


Hello,

Word, I immediately feel version 2 is easier to understand. Yeah you 
can't check the result, but you could step
through with a debugger and then check the *result*

$result just adds extra complexity; no need to copy the value and then 
return it. Just return it at the first point you can return it.
In something more complex, maybe having 50 places you could exit that 
function, in that case, save the result
and do a
 return $result;
at the bottom. That's fine, and makes sense.

I can't comment on speed but I'd guess both these methods run pretty 
close to the same speed.
I would never waste time optimizing something like this (unless I knew 
it was causing a bottleneck).

- Ben

Tod Dailey wrote:
> That's part of the reason I'm asking here, as the replies I've 
> received weren't that informative. I've heard that method #1 is a 
> left-over practice from C/Java programmers, and isn't necessary with 
> PHP (??). I've also heard the argument that returning inline values is 
> faster, and easier to see exactly what is being returned. That pretty 
> much sums up what I've heard so-far.
>
> Christian
>
>
> On Jan 11, 2008 1:02 PM, David Krings <ramons at gmx.net 
> <mailto:ramons at gmx.net>> wrote:
>
>     Tod Dailey wrote:
>     > The validity of one of my coding practices that I've used for
>     awhile has
>     > recently come under question. I've tried to listen to arguments
>     on both
>     > sides, and there seems to be good points on both ends, but my
>     question
>     > still hasn't been answered to my satisfaction.
>
>     I use version 1 (can it be that you forgot to return $result at
>     the end?)
>     where I declare anything that is used in the function at the top,
>     including
>     booleans. I then do whatever I want/need to do in that function
>     and set the
>     flag accordingly and then return the result at the very end of the
>     function
>      I see the benefits of the second version as there is a direct
>     return of the
>     state and it saves a variable. Without having it tried out any
>     other way, I
>     find version 1 to be easier to debug. In v1 you can evaluate
>     $result before
>     retruning it, can't do that with v2. V2 is potentially faster as
>     it doesn't
>     have to go through who knows how many lines of code first before
>     it reaches
>     the return at the end. As soon as a return is hit in the function
>     the function
>     exits and sends back the return values.
>
>     I'd develop using v1 and then see later if optimizing using v2
>     makes sense. It
>     would be interesting to know the pros and cons for either version
>     that you
>     have heard so far.
>
>     David
>     _______________________________________________
>     New York PHP Community Talk Mailing List
>     http://lists.nyphp.org/mailman/listinfo/talk
>
>     NYPHPCon 2006 Presentations Online
>     http://www.nyphpcon.com
>
>     Show Your Participation in New York PHP
>     http://www.nyphp.org/show_participation.php
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> New York PHP Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
>
> NYPHPCon 2006 Presentations Online
> http://www.nyphpcon.com
>
> Show Your Participation in New York PHP
> http://www.nyphp.org/show_participation.php



More information about the talk mailing list