Welcome Guest, Not a member yet? Register   Sign In
Best practice to maintain your code readability, etc.
#9

Another neat way to simplify code is to drop nested if-else statements and exit method as early as possible.

PHP Code:
public function test($input)
{
 
   if (is_integer($input)) {
 
       if ($input 100) {
 
           return $input 2;
 
       } else {
 
           if ($input 0) {
 
               return $input 3;
 
           } elseif ($input 50) {
 
               return $input;
 
           } else {
 
               return $input 4;
 
           }
 
       }
 
   }

 
   return false;


Could be simplified to:
PHP Code:
public function test($input)
{
 
   if (!is_integer($input)) {
        return 
false;
    }

    if (
$input 0) {
 
       return $input 3;
    }

 
   if ($input 100) {
 
       return $input 2;
 
   }

    if (
$input 50) {
 
       return $input;
 
   }

    return 
$input 4;

Reply


Messages In This Thread
RE: Best practice to maintain your code readability, etc. - by Pertti - 07-10-2018, 11:24 PM



Theme © iAndrew 2016 - Forum software by © MyBB