Why You Should Drop The 'Else' Keyword When Writing Conditionals
Tags: php, programming, cleancode
Published: 2023-02-09 06:08:05


Without conditionals, programming would be impossible. From rookie coders to Silicon Valley nerds, we all use conditionals each day. One common conditional is the if-else construct. I greatly reduced my use of the else statement, and this helped me to greatly reduce my code complexity, enhance readability and maintainability. Let me explain.

```php
function canDrinkAlcohol($age)
{
    if ($age !== "") {
        if ($age < 18) {
            echo "Sorry, alcohol is not for you.";
        } else {
            echo "You are free to drink alcohol.";
        }
    } else {
        echo "You must provide your age.";
    }
}
canDrinkAlcohol("");
```

The snippet above shows how most people would write their code. First, there's a check to see if the an age was supplied at all. The other condition is nested inside of that condition. 

The code looks dirty. And if you throw in other conditions to be checked for, it becomes even dirtier and more confusing to read and difficult to maintain. 

But why should you seriously consider eliminating the use of 'else' in conditionals in your programming? A look at the code above will help answer this question.

1. Because the code flow is nonlinear, it is difficult to follow as the conditions are nested.
2. It is difficult to identify which "else" corresponds to what "if."
3. Error handling becomes difficult and confusing, and this is especially so when the code blog is big.

Let's rewrite the function and eliminate the nesting and the else statements.

```php
function canDrinkAlcohol($age)
{
    if ($age == "") {
        echo "You must provide your age.";
        return;
    }
    if ($age < 18) {
        echo "Sorry, alcohol is not for you.";
        return;
    }
    echo "You are free to drink alcohol.";
    return;
}
canDrinkAlcohol(17);
```

You must have noticed a thing or two in the new function. The very first condition was reversed. I checked for the opposite of what was checked for in the first function. 

What was implemented in this new function is called the **guard clause technique**. It is derived from the fail fast method, and the purpose is to validate a condition and immediately terminate code execution by throwing a meaningful error, rather than allowing the program to throw its own errors which are often less meaningful to the average user. To terminate code execution, I introduced the **return** keyword.

The new function is structured in such a way that the program exits at the earliest point of failure. The function keeps checking for conditions and the last part of the function doesn't have a condition because for the program to get to that point, all conditions have been checked and passed.

Sometimes though, you will still absolutely need to use else statements and even nest if-else statements. In that case, you may wish to extract the nested conditionals into a separate function and chain the function or call it from within another function.

Many people are taught that what I have done above - having multiple exit points in a function is bad programming practice. Well, it's actually bad programming to write unreadable code. 

As you can see, we can easily add conditionals to the function above without getting confused. Whenever you're coding, try to eliminate nesting.