Many returns
Moderator: General Moderators
Many returns
I always have been told to have only one return per function while coding. I see many people using plenty of them in their functions, so I am wondering if it is really bad.
Any opinions?
Any opinions?
If returning early is a "special case","base case", etc. I'd say do it. It will reduce the depth of the nesting, which helps readable code and its easy to see where the special cases finish. If its more complicated, then no.
Ie, if you can write code like
then I think the multiple returns are good.
Ie, if you can write code like
Code: Select all
function foo($bar) (
if (0==$foo) return 1;
if (1==$foo) return 2;
// big nasty rest of logic
return $calcValue;
}It's the same as saying that goto, break, continue etc are evil.. They are only evil if you don't know (well enough) what they do 
http://en.wikipedia.org/wiki/Structured_programming
http://en.wikipedia.org/wiki/Structured_programming
Only having 1 return statement is a practice, I think, to correct for human error. For myself at least, if there's only 1 return statement, I'm more likely to make sure that 1 statement returns exactly what I want. In other words, I'm sure I've taken care of every condition. If I have return statements all over, then its harder for me to look through the code and be sure I've thought of everything.
It's kind of like having having a default case in a switch statement to make sure everything's handled, as opposed to just having specific cases and trying to be comprehensive with all my cases.
It's kind of like having having a default case in a switch statement to make sure everything's handled, as opposed to just having specific cases and trying to be comprehensive with all my cases.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
I don't think you can sustain that.
I grabbed the following two methods at random. In the first, I'm hunting through a list of query types looking for a match. As soon as I find one, I return some information about the query ($meta). There's no need to continue with the loop. If I don't find a match, the fn returns false.
In the second, I'm checking for database.table dot syntax in an sql query. Whatever the query string, I want an array with database and table values out of the function. However, if it's badly-formed (multiple dots) I need to return false since it's effectively unparsable.
Even with nice, short methods, you will need multiple return values at times. It could be a smell but not necessarily so.
I grabbed the following two methods at random. In the first, I'm hunting through a list of query types looking for a match. As soon as I find one, I return some information about the query ($meta). There's no need to continue with the loop. If I don't find a match, the fn returns false.
In the second, I'm checking for database.table dot syntax in an sql query. Whatever the query string, I want an array with database and table values out of the function. However, if it's badly-formed (multiple dots) I need to return false since it's effectively unparsable.
Even with nice, short methods, you will need multiple return values at times. It could be a smell but not necessarily so.
Code: Select all
/*
param (string)
return (mixed)
*/
function parse($sql, $currently_used_db)
{
$this->_currently_used_db = $currently_used_db;
foreach($this->_recognised_queries as $query) {
if(preg_match($query['pattern'], $sql, $matches)) {
if(count($meta = call_user_func_array(array(&$this, $query['fn']), array($matches)))) {
$meta['query_type'] = $query['type'];
return $meta;
}
}
}
return false;
}
// $parts[0] db name
// $parts[1] table name
function _dotSyntax($query_target)
{
$parts = explode('.', $query_target);
if(count($parts) == 1) {
array_unshift($parts, $this->_currently_used_db);
} elseif(count($parts) > 2) {
trigger_error('unparsable: multiple dots');
return false;
}
return $parts;
}
Last edited by McGruff on Fri Aug 05, 2005 9:31 pm, edited 2 times in total.
- harrisonad
- Forum Contributor
- Posts: 288
- Joined: Fri Oct 15, 2004 4:58 am
- Location: Philippines
- Contact:
Unexpected errors caused me to return early.
Code: Select all
function ValidEmail($email){
if(!$email) return false;
// rest of the code, including the last return statement
}Exactly (or maybe throw an exception in PHP5)harrisonad wrote:Unexpected errors caused me to return early.Code: Select all
function ValidEmail($email){ if(!$email) return false; // rest of the code, including the last return statement }
otherwise you end up with extra nested code
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US