DEV Community

Cover image for Replace Nested If Else Statement with Array
Timothy Soladoye
Timothy Soladoye

Posted on

6 1

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 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';
    }

}

Enter fullscreen mode Exit fullscreen mode

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';
    }
}

Enter fullscreen mode Exit fullscreen mode

Array Approach

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

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);
    }
}

Enter fullscreen mode Exit fullscreen mode

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);
}

Enter fullscreen mode Exit fullscreen mode

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (2)

Collapse
 
microerror profile image
microerror •

PHP 8.x exposes match which turns your entire article into

return match($data) {
    1 => 'one',
    2 => 'two',
   default => 'default value'
};
Enter fullscreen mode Exit fullscreen mode

There's no need for arrays or early returns, this is as concise as it gets.

Collapse
 
timoye profile image
Timothy Soladoye •

Yes @microerror ,

The match expression can also be used to replace nested if else statement

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

đź‘Ą Ideal for solo developers, teams, and cross-company projects

Learn more