PHP Coding Tips

Showing posts with label code style. Show all posts
Showing posts with label code style. Show all posts

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: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.