The Use Case for Switch Statements

Or should we just stick with the old-fashioned if-else?

Daan
Daan
Feb 14 · 4 min read
Photo by Vishnu R Nair on Unsplash.

Why would you use a switch statement instead of an if-else? That’s a great question and a sensitive topic. Every developer tends to have a strong opinion about switch statements. Some love them and use them all the time, while others never even bother messing around with them.

But is there still a valid use case for the switch statement? There are a few concerns when it comes to choosing between a switch statement and an if-else. One of them is readability and the other is speed.

Speed is often negligible since we’re talking about milliseconds. On top of that, it heavily depends on the situation. Sometimes, you might be a little bit faster using a switch statement. Other times, you might be better off using an if-else. So we’re going to focus on the readability aspect in this piece.


Switch vs. If-Else

When we start looking at this debate from a readability standpoint, things get quite interesting. We’ll go over two examples to get a sense of what the differences in code can look like between a switch statement and if-else:

As you can see in this example, the switch block seems much less chaotic and thus more readable. There are a few reasons for that.

The first is that we’re checking for the value of a single variable, and that’s exactly what a switch was made for. You might have also noticed that the OR check is way more elegant in the switch statement.

The second reason is that as the logic chain gets larger — and with five else-ifs, we can say it’s pretty large — the if-block tends to get really messy. Part of this has to do with the number of curly brackets that we need to use to get this piece of code to work. When it comes to large logic chains, the switch statement seems to be much more readable.

In the second example of switch versus if-else, we are comparing a string, which is not a great use case for the switch statement most of the time.

If the data type is usable for a switch and all options have constant values, you should always use — or at least take it into serious consideration — a switch when you have at least two options to differentiate between.

Most of the time when you’re comparing a string, all options don’t have constant values, which is why this isn’t a good use case for a switch statement.

Here’s my take on switch statements: I’d use a switch every time if I’m switching on the value of a single variable with at least two options with constant values to differentiate between. That’s what the switch construct was made for. Otherwise, stick with multiple if-else statements.

Another reason some programmers use switch statements is that they find them more pleasing aesthetically. This can be a good reason to use the switch statement as well since it makes the code more readable to you.

Personally, I like combining the switch statement with an enumeration since it’s very readable. The following example just looks very clean to me:


What About You?

Are you the kind of programmer who loves switch statements because there are situations where it makes your code more readable? Or do you avoid using switch statements in your code?

In the end, there isn’t a definitive answer to the question of whether a switch statement or if-else is better to use. There’s nothing wrong with sticking to if-else constructions in certain situations.

I’m just very interested in your point of view on this topic, so please share your thoughts. Let’s have a chat about the switch statement and its use cases!

Better Programming

Advice for programmers.

Daan

Written by

Daan

Backend developer from The Netherlands. Crypto enthusiast.

Better Programming

Advice for programmers.

More From Medium

More from Better Programming

More from Better Programming

More from Better Programming

Why Do Incompetent Managers Get Promoted?

1.1K

More from Better Programming

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade