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 May 30, 2007, at 6:32 AM, Richard Davey wrote:

Hi Dave,

Wednesday, May 30, 2007, 12:20:48 PM, you wrote:

If there is no need to return a value then I don't do so. However, the function is going to process something, and surely you should check that the
processing has succeeded or failed?

I have exception and error handling dealt with fully in my functions,
by which stage the 'return' at the end becomes redundant because the
return value doesn't need checking as the error handler has already
taken over. However take the following:

$result = $this->calculateSomething($value);

If 'calculateSomething' has all the error handling it requires built
into it, then isn't checking the value of 'result' superfluous to
requirements? Yet even so, I still like to return something at the end
regardless :)

I normally (but not always) return (whether I will actually use that value or not).


I guess another way to phrase the same question would be where do you
shift all of your error handling - inside the function itself, or in
the code that calls it (i.e. checking the $result in the example
above). Personally I handle it all in the function otherwise I'm
duplicating masses of result checking.

I spent some time thinking about this exact question and I came up with several things:

1. How does PHP do it?

They have a function which performs whatever and returns an error (code) upon failing. This allows every developer to deal with the error in their own way.

2. Does this function need to be portable? (Short answer, yes, it *should* be.)

Take these functions for example:

<?
function doSomethingToA ($a)
{
   if (!$a) $_SESSION["not_portable"] = false;
   else $_SESSION["not_portable"] = true;
   return; // optional
}

function doSomethingToB ($b)
{
   if (!$b) return false;
   else return true;
}

// Non-portable function that probably won't work outside this application
doSomethingToA ($_SESSION["whatever"]);

// Portable function that you can take anywhere! =D
$_SESSION["portable"] = doSomethingToB ($_SESSION["whatever"]);
?>

Yes, you may have to do some more error-checking on the outside of the function. However, the question comes down to... what's your use for it. It's almost a religious question - it's up to you on how you code. Just be sure to weigh all the options.

~Philip


It isn't a case of wrong/right, just trying to gauge preferences here.

Cheers,

Rich

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