Re: Multiple return statements in a function.
| From: | Per Jessen | Date: | Thu, 23 Apr 2009 13:20:13 +0000 |
| Subject: | Re: Multiple return statements in a function. | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-291864@lists.php.net to get a copy of this message | ||
George Larson wrote:
> That's an interesting subject that I've never considered.
>
> I usually return immediately. For me, it makes the code easier to
> read. I work with a number of other coders here and, if the result
> isn't returned then I have to keep reading through the code to make
> sure nothing else is done with it. However, when I see the 'return'
> then I know we're done there.
I tend to try to have just one return point, but I will occasionally
have more, typically when a function returns more than just true or
false.
If it's a true or false outcome, but I still have multiple failure
points, I'll sometimes use a construct like this:
rc=0;
while(true){
if ( cond1 ) { errormsg; rc=1; break; }
if ( cond2 ) { errormsg; rc=1; break; }
if ( cond3 ) { errormsg; rc=1; break; }
if ( cond4 ) { errormsg; rc=1; break; }
if ( cond5 ) { errormsg; rc=1; break; }
if ( cond6 ) { errormsg; rc=1; break; }
if ( cond7 ) { errormsg; rc=1; break; }
break;
}
return rc;
--
Per Jessen, Zürich (16.6°C)