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

From: Date: Wed, 30 May 2007 13:45:53 +0000
Subject: Re: Return or not to return, that is the question
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-255807@lists.php.net to get a copy of this message
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?

« previous php.general (#255807) next »