function compute( $a , $b ) {
if ( $a <= 5 ) {
// outpu error
return;
}
if ( $b >= 3 ) {
// output error
return;
}
// logic
}
As you can see, second solution is much more clear.
PHP Coding tips & tricks. Function & error explanation, coding style, how tos etc. Short notes posted directly from developer work environment.
PHP Coding Tips
Tuesday, August 7, 2012
Organize your input checking and error reporting. Escape from too many inner if's
It's common to check some variables and report error / unsucceful operation if value is bad or to check result of operations before going forward.
It can provide a bad code structure, where you can't find yourself in middle of a ton of bracets.
Try to use return or exit instead, this will provide a cleaner, maintable code:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment