Return or not to return, that is the question

This is a discussion on Return or not to return, that is the question within the PHP General forums, part of the PHP Programming Forums category; Hi all, Just a quick straw-poll really: What is your take on using 'return' when you end a function, ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-30-2007
Richard Davey
 
Posts: n/a
Default Return or not to return, that is the question

Hi all,

Just a quick straw-poll really:

What is your take on using 'return' when you end a function, if you
don't actually need to return a value?

If you have to return say a true/false as the result of an operation,
then it's an obvious choice. But what if all the function does is
perform an action and then quit? Do you like to use 'return' at the
end of it anyway, or do you just let it run into the closing } ?

Or do you perhaps do a 'return true' at the end, regardless, even if
the rest of your code never checks that value (on the basis that it
may do in the future)

Cheers,

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

"Never trust a computer you can't throw out of a window"
Reply With Quote
  #2 (permalink)  
Old 05-30-2007
M. Sokolewicz
 
Posts: n/a
Default Re: Return or not to return, that is the question

Richard Davey wrote:
> Hi all,
>
> Just a quick straw-poll really:
>
> What is your take on using 'return' when you end a function, if you
> don't actually need to return a value?
>
> If you have to return say a true/false as the result of an operation,
> then it's an obvious choice. But what if all the function does is
> perform an action and then quit? Do you like to use 'return' at the
> end of it anyway, or do you just let it run into the closing } ?
>
> Or do you perhaps do a 'return true' at the end, regardless, even if
> the rest of your code never checks that value (on the basis that it
> may do in the future)
>
> Cheers,
>
> Rich


In such cases it depends on the project I'm working on. If it's a
slightly bigger project which will see future development, I usually
stick to a return true|false policy even though such functions don't
have a meaningful return (YET!), as this might change later.
If it's a project which will be used but probably won't see any
extensive future development, I generally tend to not return anything.

- Tul
Reply With Quote
  #3 (permalink)  
Old 05-30-2007
Edward Kay
 
Posts: n/a
Default RE: [PHP] Return or not to return, that is the question



> Just a quick straw-poll really:
>
> What is your take on using 'return' when you end a function, if you
> don't actually need to return a value?
>
> If you have to return say a true/false as the result of an operation,
> then it's an obvious choice. But what if all the function does is
> perform an action and then quit? Do you like to use 'return' at the
> end of it anyway, or do you just let it run into the closing } ?
>
> Or do you perhaps do a 'return true' at the end, regardless, even if
> the rest of your code never checks that value (on the basis that it
> may do in the future)
>


I like to have all my functions return something so use return true.

Edward
Reply With Quote
  #4 (permalink)  
Old 05-30-2007
Dave Goodchild
 
Posts: n/a
Default Re: [PHP] Return or not to return, that is the question

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?

Reply With Quote
  #5 (permalink)  
Old 05-30-2007
Richard Davey
 
Posts: n/a
Default Re[2]: [PHP] Return or not to return, that is the question

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

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

Cheers,

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

"Never trust a computer you can't throw out of a window"
Reply With Quote
  #6 (permalink)  
Old 05-30-2007
Paul Scott
 
Posts: n/a
Default Re: [PHP] Return or not to return, that is the question


On Wed, 2007-05-30 at 12:20 +0100, Dave Goodchild 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?


If you unit test, then returns become quite important, so I almost
always return;

--Paul


All Email originating from UWC is covered by disclaimer http://www.uwc.ac.za/portal/uwc2006/...imer/index.htm

Reply With Quote
  #7 (permalink)  
Old 05-30-2007
Zoltán Németh
 
Posts: n/a
Default Re: [PHP] Return or not to return, that is the question

2007. 05. 30, szerda keltezéssel 11.52-kor Richard Davey ezt Ã*rta:
> Hi all,
>
> Just a quick straw-poll really:
>
> What is your take on using 'return' when you end a function, if you
> don't actually need to return a value?
>
> If you have to return say a true/false as the result of an operation,
> then it's an obvious choice. But what if all the function does is
> perform an action and then quit? Do you like to use 'return' at the
> end of it anyway, or do you just let it run into the closing } ?
>
> Or do you perhaps do a 'return true' at the end, regardless, even if
> the rest of your code never checks that value (on the basis that it
> may do in the future)


personally I prefer to use return only if I need the return value for
something. If it's not used then I think it's just a waste of code lines
and resources to return anything...

greets
Zoltán Németh

>
> Cheers,
>
> Rich
> --
> Zend Certified Engineer
> http://www.corephp.co.uk
>
> "Never trust a computer you can't throw out of a window"
>

Reply With Quote
  #8 (permalink)  
Old 05-30-2007
Richard Lynch
 
Posts: n/a
Default Re: [PHP] Return or not to return, that is the question

On Wed, May 30, 2007 5:52 am, Richard Davey wrote:
> Just a quick straw-poll really:
>
> What is your take on using 'return' when you end a function, if you
> don't actually need to return a value?
>
> If you have to return say a true/false as the result of an operation,
> then it's an obvious choice. But what if all the function does is
> perform an action and then quit? Do you like to use 'return' at the
> end of it anyway, or do you just let it run into the closing } ?
>
> Or do you perhaps do a 'return true' at the end, regardless, even if
> the rest of your code never checks that value (on the basis that it
> may do in the future)


Planning for a return value that you might need but have no idea what
it will be is probably a Bad Idea -- You'll just need to document it,
maintain it, etc for no real reason.

If the function is only called for side-effects (I.e., it returns
nothing) then don't return anything -- You'll know by looking at the
end of the function that it's not supposed to return anything.

Adding the gratuitous 'return' seems of dubious benefit.

I rarely write a function that doesn't return anything, come to think
of it...

--
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?
Reply With Quote
  #9 (permalink)  
Old 05-30-2007
Philip Thompson
 
Posts: n/a
Default Re: Re[2]: [PHP] Return or not to return, that is the question

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

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 03:48 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0