NYCPHP Meetup

NYPHP.org

[nycphp-talk] Question about explicit returns

David Krings ramons at gmx.net
Fri Jan 11 13:02:43 EST 2008


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



More information about the talk mailing list