Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reduce the number of if-else statements in PHP?

I found that there are many if-else statements, especially nested if else statements, these statements make my code less readable. How to reduce the number of if else statements in PHP?

My tips are as follows: 1.Use a switch statement when it is suitable; 2.use exit() statement when it is feasible; 3. Use ternary statement when it is feasible;

Are there other tips that can reduce if else statements, especially nested if-else statements?

like image 597
Steven Avatar asked Nov 26 '09 15:11

Steven


People also ask

How do you shorten if-else statements?

The ternary operator, also known as the conditional operator, is used as shorthand for an if...else statement. A ternary operator is written with the syntax of a question mark ( ? ) followed by a colon ( : ), as demonstrated below. In the above statement, the condition is written first, followed by a ? .

How many else if statements can you have?

One of the statements runs when the specified condition is True, and the other one runs when the condition is False. When you want to define more than two blocks of statements, use the ElseIf Statement. You can nest up to ten levels of If... Then... Else statements.

Can you have multiple else if statements PHP?

You can use any number of elseif statements. In the following example, we use a number of elseif statements to display different messages in different conditions. Also, you will notice that the else statement at the end is not necessary for this.


3 Answers

Refactor your code into smaller work units. Too much conditional logic is a code-smell and usually indicates that your function needs to be refactored.

like image 111
Jeff Paquette Avatar answered Oct 16 '22 06:10

Jeff Paquette


There is an official academic method to refactor and simplify a lot of if conditions, called Karnaugh mapping.

It takes in multiple test conditions and attempts to assist in creating simplified if statements that cover all the required cases.

You can learn more about it from wiki here.

like image 23
iWantSimpleLife Avatar answered Oct 16 '22 08:10

iWantSimpleLife


I work on a lot of code thats full of ever evolving business logic and needs to be modified every other day. Two tips that's certainly helped me keep up with the modifications are: avoid all else statements and return/exit as soon as possible. Never get into deep nesting -> create sub routines/functions.

Replacing all else statements with negated if statements makes your code much easier to read top to bottom (the proximity of the condtion and the code block):

# business logic block
if ( $condition ) {
    # do something
    # code code code
} else {
    # code code code
    return;
}

# refactored:
if ( ! $contition ) {
    # code code code
    return;
}
if ( $condition ) {
    # code code code 
}

Secondly, return/exit as soon as possible. My opinion of course, but I don't see the point in running through any extra conditions/tests when once you've already determined the result of the subroutine, especially when you would like to read the code top to bottom. Removing all ambiguity makes things simpler.

To conclude, I like to avoid using else especially in long lists of BL. Return as soon as you know the result. If the nesting level is more than 2, create sub routines/functions.

like image 31
smassey Avatar answered Oct 16 '22 06:10

smassey