Avoid the Use of else in Programming

Avoid the Use of else in Programming

In the world of programming, the if-else statement is one of the fundamental tools for controlling the flow of a program's execution, taught in the very first lessons. However, it's time to look at it from a different angle and, possibly, learn to get by without the else part.

Single Responsibility Principle

The use of else often signals that a function is performing more than one task, which violates the Single Responsibility Principle. This can lead to errors and complicate the logic of the program.

How to Avoid else

There are several ways to avoid using else in your functions. One is the use of guard clauses, another is applying default values.

Example from a real development

Suppose we have a function that checks whether a user in a CRM system is active. If the user is active, the function returns their data; otherwise, it returns a standard set of data.

function getUserData($userID) {

    $user = findUserByID($userID);

    if ($user->isActive) {

        return $user->getData();

    } else {

        return getDefaultUserData();

    }

}

This code uses else, but we can improve it:

function getUserData($userID) {

    $user = findUserByID($userID);

    if (!$user->isActive) {

        return getDefaultUserData();

    }

    

    return $user->getData();

}

 

Now, the function first checks if the condition for returning the default value is met. If so, it immediately returns this value. Otherwise, the main logic of the function continues.

Improvement Using the Ternary Operator

The ternary operator is another way to simplify code traditionally composed of if-else. It is particularly useful for assignments and returns:

function getUserStatus($userID) {

    $user = findUserByID($userID);

    return $user ? $user->status : 'guest';

}

In this example, depending on whether the user is found, the function returns either their status or the 'guest' status.

The ternary operator should be used cautiously and only in cases where it genuinely simplifies the code, not complicates its understanding.

Conclusion

Avoiding else in programming is not an absolute rule, but it is a good practice that can make your code more readable and reliable. By applying the described approaches and techniques, you will be able to write more efficient and understandable code, which is especially important in large and complex projects, such as Customer Relationship Management (CRM) systems.

Popular Posts

My most popular posts

Maximum productivity on remote job
Business

Maximum productivity on remote job

I started my own business and intentionally did my best to work from anywhere in the world. Sometimes I sit with my office with a large 27-inch monitor in my apartment in Cheboksary. Sometimes I’m in the office or in some cafe in another city.

Hello! I am Sergey Emelyanov and I am hardworker
Business PHP

Hello! I am Sergey Emelyanov and I am hardworker

I am a programmer. I am an entrepreneur in my heart. I started making money from the age of 11, in the harsh 90s, handing over glassware to a local store and exchanging it for sweets. I earned so much that was enough for various snacks.

Hire Professional CRM developer for $25 per hour

I will make time for your project. Knowledge of Vtiger CRM, SuiteCRM, Laravel, and Vue.js. I offer cooperation options that will help you take advantage of external experience, optimize costs and reduce risks. Full transparency of all stages of work and accounting for time costs. Pay only development working hours after accepting the task. Accept PayPal and Payoneer payment systems. How to hire professional developer? Just fill in the form

Telegram
@sergeyem
Telephone
+4915211100235