Share
Preview
How to avoid falling deeper and deeper down into the IF/ELSE deadly spiral.
 ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌
ALEX WEB DEVELOP

Alex
The IF/ELSE Deadly Spiral
by Alex
Spiral


Hey,
Alex here.

Does this ever happen to you?
You create an IF block. Then, inside this block you need to create another one.
And then you realize that, inside the second one, you actually need a third one… and so on.
Deeper and deeper down into the IF/ELSE deadly spiral.

Until you end up with a piece code so complex, that it looks like the Matrix itself:

if ($a) {
   if ($b) {
      if ($c) {
         if ($d) { //...
         }

          else if ($e) { //...
         }
      }
      else { //...
      }
   }
}


It did happen to you too, didn’t it?

Of course, it's better to avoid such complex IF/ELSE blocks.
For three reasons:
  • They make your code really hard to understand.
  • Changing the IF/ELSE logic, or adding a new condition, becomes too difficult and error-prone.
  • With too many different cases, you are likely going to duplicate some code.

So, let’s see 3 techniques to avoid the IF/ELSE deadly spirals in the first place.


1. Use multiple return exit points.

This is the easiest technique to use in functions and class methods, where the different IF/ELSE cases select the value to be returned.
The idea is to return the value immediately after each condition, so that the next conditions are automatically excluded.

For example:

if ($firstCondition) {
   return $firstValue;
}
if ($secondCondition) {
   return $secondValue;
}
return $thirdValue;


This solution helps avoiding IF/ELSE nesting in those cases where each condition excludes the others.
By returning the value right away, the next code block is only executed if the first condition is false.

I don’t really like this solution, though.
If you remember my "clean code" tips, you know that I prefer to have a single, final return point.
Moreover, with multiple return statements it is hard to understand what value the function returns.

Which brings us to the second solution.


2. Use a NULL-initialized value.

Here it is a better implementation of the previous solution.
First, you initialize the return value to NULL.
Then, instead of nesting multiple IF/ELSE conditions, you use the return value as a sentinel.

For example:

$return = NULL;
if ($firstCondition) {
   $return = $firstValue;
}
if (is_null($return) && $secondCondition) {
  $return = $secondValue;
}
if (is_null($return) && $thirdCondition) {
  $return = $thirdValue;
}

return $return;


The advantages over the previous solution is that you have only a single, final return point. On top of that, this solution works if you are not inside a function or class method.


3. Refactor your code into functions.

Finally, let's see a more radical approach.
Depending on the specific logic of your IF/ELSE blocks, you can implement the condition checks into functions.
Then, you can replace the IF/ELSE tree with calls to these functions. Each function will perform a condition check and return the proper value.

For example:

$value = funct1($cond1, $cond2);
$value = funct2($value, $cond3);
$value = funct3($value, $cond4);


Each function can take the current $value as well as one or more additional conditions ($cond1, $cond2, $cond3...) as arguments and return a new value.
You can also group the whole procedure into a single function containing all the above function calls.

Here is a concrete example.
Let's say that you want to know how many days a year has.
To check whether a year is a leap year, you need to check that it is divisible by 4, but not by 100 unless it is also divisible by 400.

Instead of using IF/ELSE conditions, you can perform the following function calls:

$divBy4 = divisibleBy4($year);
$divBy100 = divisibleBy100($year);
$divBy400 = divisibleBy400($year);
$isLeap = isLeapYear($divBy4, $divBy100, $divBy400);


The isLeapYear() function returns true if $divBy4 is true and either $divBy400 is true or $divBy100 is false.
You don't need any IF at all in all those functions.
Even the last check is easy enough to be done in a single line:

return $divBy4 && (!$divBy100 || $divBy400);



That's all for today.
Now it's your turn: send me a reply with your questions and thoughts.

Until next time,
Alex



Share the knowledge

Did you like this email? Share it with your friends and colleagues.
Click here to share it



You Don’t Want Your PHP Apps to be Hacked?
To make sure it will not happen, you need to know:
  • Which attacks you must prevent.
  • The specific defense techniques to stop each of those attacks.

PHP Security Mastery
is my security course that will teach you exactly how to do that.

Click here for the details.


Need help with your PHP code?
If you can't make your PHP code work, you can ask me for a Code Review.
I will verify your code, fix it and make sure it works the way you want.

Click here for the details.


Resources

Alex Web Develop - My blog where you can find my tutorials.
Alex PHP café - My Facebook group where you can talk with me and other developers.

Background vector created by GarryKillian - www.freepik.com



You are receiving this newsletter because you subscribed to Alex Web Develop.

If you unsubscribe, you will not get any more emails from me.

Alessandro Castellano, P.IVA (VAT ID): 07012140484, via Luigi Morandi 32, 50141 Firenze FI, Italy

Email Marketing by ActiveCampaign