Home
Do you avoid using if-else instead of switch block?
  v1.0 Posted at 1/05/2018 8:18 AM by Tiago Araujo

The .NET framework and the C# language provide two methods for conditional handling where multiple distinct values can be selected from. The switch statement is less flexible than the if-else-if tree but is generally considered to be more efficient. .NET compiler generates a jump list for switch block for that reason it is faster for long list of condition. And also complier has the ability to optimize the switch block. Condition in the switch block is faster as compiler evaluated the condition once but for if-else block, compiler need to evaluate the condition for each 'else if' block

Note: Performance is very much negligible when number of condition is less than 5. So if the code design is clearer & easily maintainable by using if-else block, then Developer should use if-else block for fewer conditions.


int DepartmentId = GetDepartmentId()
if(DepartmentId == 1)
{
// do something
}
else if(DepartmentId == 2)
{
// do something #2
}
else if(DepartmentId == 3)
{
// do something #3
}
else if(DepartmentId == 4)
{
// do something #4
}
else if(DepartmentId == 5)
{
// do something #5
}
else
{
// do something #6
}

Figure: Bad example of coding practice


int DepartmentId = GetDepartmentId()
switch(DepartmentId)
{
case 1:
// do something
break;
case 2:
// do something # 2
break;
case 3:
// do something # 3
break;
case 4:
// do something # 4
break;
case 1:
// do something # 5
break;
case 1:
// do something # 6
break;
default:
//Do something here
break;
}

Figure: Good example of coding practice which will result better performance

Further Reading: Speed Test: Switch vs If-Else-If

Related rules

    Do you feel this rule needs an update?

    If you want to be notified when this rule is updated, please enter your email address:

    Comments:

    Note: Social Media login for Yotpo is not working in IE or Safari, please use Chrome. We are waiting for Yotpo to fix it.