Re: Re: Early return (was: Inspiration for a Tombstone.)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 <rcostu@xxxxxxxxx> 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 <tedd.sperling@xxxxxxxxx> 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
>>
>>
>

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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux