Replace Nested If Else Statement with Array

Replace Nested If Else Statement with Array

The use of Early return and Array Approach will be considered

Nested if else

I am very sure we have either written or seen such nested code.

It isn’t pleasant to look at.

public fun…


This content originally appeared on DEV Community and was authored by Tim Oye

Replace Nested If Else Statement with Array

The use of Early return and Array Approach will be considered

Nested if else

I am very sure we have either written or seen such nested code.

It isn't pleasant to look at.

public function checkData($var){
    if (isset($var)){
          if ($var==1){
              return 'one';
          }
          elseif($var==2){
              return 'two';
          }
          elseif($var==3){
              return 'three';
          }
          elseif($var==4){
              return 'four';
          }    
    }
    else{
     return 'null';
    }

}

We can make it better

Early Return

public function checkData($var){
    if (!isset($var)){
        return 'null';
    }
    if ($var==1){
        return 'one';
    }
    if($var==2){
        return 'two';
    }
    if($var==3){
        return 'three';
    }
    if($var==4){
        return 'four';
    }
}

Array Approach

public function checkData($var){
    $array = [
    '1'=>'one',
    '2'=>'two',
    '3'=>'three',
    '4'=>'four',
    ];
    return $array[$var] ?? 'null'; 
}

The array approach is easy to update and easier to understand as compared to the nested If Else.

The early return also works very fine!

The Array approach can also be used for methods.

Early return Method calling

public function checkData($var){
    if (!isset($var)){
        return $this->nullMethod($var);
    }
    if ($var==1){
        return $this->oneMethod($var);
    }
    if($var==2){
        return $this->twoMethod($var);
    }
    if($var==3){
        return $this->threeMethod($var);
    }
    if($var==4){
        return $this->fourMethod($var);
    }
}

Array Approach Method calling

public function checkData($var){
 $array_methods = [
    '1'=>'oneMethod',
    '2'=>'twoMethod',
    '3'=>'threeMethod',
    '4'=>'fourMethod',
    ];
    return $this->{$array_methods[$var]($var)};

    //to simplify, break it down
    $method=$array_methods[$var];
    return $this->{$method}($var);
}


This content originally appeared on DEV Community and was authored by Tim Oye


Print Share Comment Cite Upload Translate Updates
APA

Tim Oye | Sciencx (2022-07-12T22:09:04+00:00) Replace Nested If Else Statement with Array. Retrieved from https://www.scien.cx/2022/07/12/replace-nested-if-else-statement-with-array/

MLA
" » Replace Nested If Else Statement with Array." Tim Oye | Sciencx - Tuesday July 12, 2022, https://www.scien.cx/2022/07/12/replace-nested-if-else-statement-with-array/
HARVARD
Tim Oye | Sciencx Tuesday July 12, 2022 » Replace Nested If Else Statement with Array., viewed ,<https://www.scien.cx/2022/07/12/replace-nested-if-else-statement-with-array/>
VANCOUVER
Tim Oye | Sciencx - » Replace Nested If Else Statement with Array. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/07/12/replace-nested-if-else-statement-with-array/
CHICAGO
" » Replace Nested If Else Statement with Array." Tim Oye | Sciencx - Accessed . https://www.scien.cx/2022/07/12/replace-nested-if-else-statement-with-array/
IEEE
" » Replace Nested If Else Statement with Array." Tim Oye | Sciencx [Online]. Available: https://www.scien.cx/2022/07/12/replace-nested-if-else-statement-with-array/. [Accessed: ]
rf:citation
» Replace Nested If Else Statement with Array | Tim Oye | Sciencx | https://www.scien.cx/2022/07/12/replace-nested-if-else-statement-with-array/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.