Created
November 18, 2010 18:52
-
-
Save gavinblair/705417 to your computer and use it in GitHub Desktop.
Which method is better?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function myfunction(){ | |
| if($somecondition) { | |
| ... | |
| ... //many lines of code | |
| ... | |
| } else { | |
| return false; | |
| } | |
| } | |
| /* OR */ | |
| function myfunction(){ | |
| if(!$somecondition) { | |
| return false; | |
| } | |
| ... | |
| ... //many lines of code | |
| ... | |
| } |
They aren't the same, one will return false, one will not
Author
@SeanJA how about now?
Me thinks...
<?php
//multiple returns (possibility of deleting one by accident)
function myfunction(){
if($somecondition) {
...
... //many lines of code
...
return $return;
}
return false;
}
/* OR */
//multiple returns (possibility of deleting one by accident)
function myfunction(){
if(!$somecondition) {
return false;
}
...
... //many lines of code
...
return $return;
}
// or ---
//single return point
function test(){
$return = false;
if($condition){
//many lines of code
$return = $somethingElse;
}
return $return;
}
Author
The main issue was wrapping 99% of your function in an IF means there is the possibility of deleting one of the }'s by accident.
replace //many lines of code with //function_call :P
Author
haha ya. really, if the entire function is an if statement, then the if could be put around the call.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I like the second one better, because it's harder to lose the if's closing bracket :)