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

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

 



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.

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

Cheers,

Rich
-- 
Zend Certified Engineer
http://www.corephp.co.uk

"Never trust a computer you can't throw out of a window"

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