Switch vs if-else

Despite the fact that PHP Manual suggests to use switch statement to match single variable I would argue that switch is in general better for any if-else code that has at least three branches. That is, suppose that you have:

// this is a magical script that does all work for you
if (today_is_weekend()) {
    relax();
} elseif (today_is_friday() || tomorrow_is_a_holiday()) {
    work_half_time();
} else {
    work_full_time();
}

then you should start rewriting you code into switch because it will look much more simple and clean.

But you probably won’t. Because you are afraid of switch statements for their easy-to-forget break-s. Or because it’s an obsolete construction that only C++ nerds know of. Or just because you don’t know how to rewrite a code in such way that it uses switch.
Let’s look first how you can do it:

// this is a magical script that does all work for you
switch (true) {
    case today_is_weekend():
        relax();
    break;
    case today_is_friday():
    case tomorrow_is_a_holiday():
        work_half_time();
    default:
        work_full_time();
}

Is it looking nicer?
No? Why?
Okay, okay it’s now 10 lines instead of 7. And it’s incorrect because I missed a break; after line 7. But let’s look what we got instead.

Notice that each condition is now on a separate line so we can easily reconstruct the logic of the script. They are also aligned and there are plenty of space to the right for inline comments should they be needed.

Let’s now look at more complicated example. We now take into account vacations and holidays and illnesses. Also suppose, that we have two types of companies: in one company state holidays are automatic day-offs, whilst in the other one employees may either work half-time or take a vacation. Actually it’s the moment when someone might start creating classes and objects but let’s for a moment look how switch will solve it.

switch (true) {
    case today_is_weekend():
    case its_holiday() && $MyCompany->off_on_holidays():    
    case $Me->in_a_vacation():
    case $Me->is_ill():
        relax();
    break;

    case today_is_friday():
    case tomorrow_is_a_holiday():
    case its_holiday() && ! $MyCompany->off_on_holidays():    
        work_half_time();
    break;        
    
    default:
        work_full_time();
}

We just added three lines and “Boom” – it’s done. If-Else code:

if (today_is_weekend() 
	|| its_holiday() && $MyCompany->off_on_holidays()
	|| $Me->in_a_vacation()
	|| $Me->is_ill()
   ) {
    relax();
} elseif (today_is_friday() 
	|| tomorrow_is_a_holiday()
	|| its_holiday() && ! $MyCompany->off_on_holidays()
   ) {
    work_half_time();
} else {
    work_full_time();
}

Which one is now easier to read? My opinion is that switch code is more elegant and easier to extend if you want to add more conditions.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.