Home > Refactorings > Refactoring multi-conditional statements: the switch statement and the “if-else-if” ladder

Refactoring multi-conditional statements: the switch statement and the “if-else-if” ladder

April 30th, 2012

There are two methods for multi-conditional processing: the “if-else-if” ladder and the switch statement. The If-Else-If ladder is a combination of ‘if’ and ‘else’ statements that is used to test a series of conditions. If the condition of the first ‘if’ statement is met, the code within the ‘if’ executes. Otherwise, the program flow is passed to the neighborhood ‘else’ statement, in which the next ‘if’ statement is located. This continues as a series of ‘if’ statements nested within the previous ‘else’ statements until all conditions have been checked. A default branch or a code block may be executed in a final ‘else’ statement if no condition is previously met.

The switch statement is similar to the ‘if-else-if’ ladder as it provides multiple branching. In the case of the switch statement, the value of a variable or expression is tested against a series of different values (cases), until a match is found. The code block within the matched case statement is executed. If none of the cases match, then the optional default case is executed.

We can find the following differences between these two multi-condition evaluation approaches:

  • the switch statement is generally considered easier to read;
  • the switch statement is less flexible than the if-else-if ladder, because it only permits testing of a single expression against a list of discrete values;
  • the switch statement is generally considered to be more efficient, because the compiler is able to optimize the switch statement. The compiler may re-order the testing to provide the fastest execution, because each case within a switch statement does not rely on previous cases. In the case of the if-else-if ladder, the code must process each if statement in the order determined by the developer.

All in all, it is up to you to choose the option that makes the code more readable and maintainable and which approach to use: the “if-else-if” ladder or the switch statement. To help you easily convert one approach to another and vice versa CodeRush bundled with Refactor! Pro contains two refactorings:

  • Conditional to Case
  • Case to Conditional

The Case to Conditional refactoring converts the switch (C#, C++) or Select (VB) statement into a combination of if/else statements, e.g.:

DevExpress Case To Conditional refactoring

Result:

DevExpress Case To Conditional refactoring result

The Conditional to Case refactoring is the opposite of the Case to Conditional refactorings which converts a set of if/else statements into a switch/Select statement:

DevExpress Conditional to Case refactoring

Both refactorings are available in C#, Visual Basic and C++ languages.

—–
Products: Refactor! Pro
Versions: 12.1 and up
VS IDEs: 2008 and up
Updated: May/05/2012
ID: R054

Similar Posts: