Re: Re[2]: Return or not to return, that is the question

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

 



On Wed, May 30, 2007 10:25 am, Richard Davey wrote:
> Hi Paul,
>
> Wednesday, May 30, 2007, 4:07:00 PM, you wrote:
>
>> I demur at your final point:  If we don't use exit() and the
>> function
>> performs non-aborting error handling, it's going to return to the
>> calling function which in most cases will need to know whether its
>> child function succeeded or failed.
>
>>          function parent()
>>          {
>>                  lookUpData();
>>                  displayData();
>>          }
>>          function lookUpData()
>>          {
>>                  set up query;
>>                  execute query;
>>                  handle errors;
>>          }
>
>> where "handle errors" might range from returning a failure flag to
>> displaying an error message.
>
> There's a world of difference between those two events though. If all
> 'handle errors' does is to return an error flag, then the parent
> obviously *needs* to check it. Equally all other functions that ever
> call lookUpData() need to duplicate those checks too.
>
>> In order that displayData() doesn't fall on its face, I would write
>> the parent function in one of these ways:
>
>>          if (lookUpData()) displayData();
>
> That's where our approach differs. If lookUpData falls flat on its
> face, my error handler will take over completely, finally resulting in
> an 'abortive' event, and never pass back to the parent. If an error is
> of a critical enough nature the system needs to stop. If it's not
> critical then the error handling within displayData() would detect it
> has nothing to display and error in its own accord.

This sounds perfectly reasoanble.

>> In my programming style, I can't imagine wanting to write this code
>> in such a way that lookUpData() didn't return some form of success
>> or
>> error indicator.
>
> That's a *very* specific example though. My question was do people
> place a 'return' statement at the end of **ALL** of their functions,
> regardless of what that function actually did. In the code you gave
> there is a fair argument both ways, but that isn't always the case.
>
> Here's a piss-poor example off the top of my head:
>
> function parent()
> {
>          display()
> }
>
> function display()
> {
>          echo something random
> }
>
> In this instance (albeit gloriously simple / useless), would your
> display() return true even though it could have never actually failed?
> and if it did, do you then care about checking that value in the
> parent?

Technically, there is no such thing as "could have never actually
failed"...

It's not outside the realm of possibility that:
echo "something random";
could actually fail...

In all the ways I can think of, PHP would never actually return, but
there's no guarantee that won't change under the hood tomorrow...

I'm not saying that every scripter should worry about this; but if you
have a mission-critical application in PHP, you should probably go
ahead and return true/false on success/failure of every function, no
matter how trivial it may seem.

But for your basic website, no, you don't need that level of
robustness -- The Internet itself will cause so many failures that
your lack of error-checking for something that rare will be lost as
"noise" in any sort of statistical error model.

-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
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