Switch is ok
Pragmatic Maciej
Posted on May 8, 2020
Switch statement had lately some bad fame, some developers avoid it as possible, and others advocate to deprecate it. Almost as if it would be some anti-pattern to use it. To make you an example of the attitude, here are few articles:
- Eliminate the switch statement for better code
- Deprecating the switch statement
- Replace your switch statement and multiple "if and else", using Object Literals
This article will try to fight with common statements against switch. So here they go!
Switch is old and clunky
Yes Switch statement is old, if statement also is old, for loop is old too, but so what? Really what a strange argument it is. New and shiny doesn't mean better.
We're forced to manually add break
That's a kinda interesting take, as in all examples criticizing switch, the switch is shown with a break keyword. It is so by purpose, to make the counterexample looking better. So no, we don't need to use break, we can use switch with return only.
switch (x) {
case "DC":
return "Batman";
case "Marvel":
return "Wolverine";
default:
return "Spiderman"
}
Where is the break now?
Its not an expression
Yes, it's not. But wrapping it in a function will give us expression:
function hero(x) {
switch (x) {
case "DC":
return "Batman";
case "Marvel":
return "Wolverine";
default:
return "Spiderman"
}
}
const myHero = hero("DC");
It is emphasizing impure and procedural programming
You work with multi-paradigm language, where you can mutate everything, you can make any side effect whenever you want, and still make such argument? Purity in such language like JS is a coding practice only. If you want to write pure code, switch will not be any blocker, believe me.
It is error prone and likely to make some mistake
Really stop using Notepad. Please.
And update your Windows 8.
Use object literals instead of switch
Yes you can, I will not say that using literal is a bad idea. But saying that switch is much worse, is still overstatement. Consider both examples:
// switch
function hero(x) {
switch (x) {
case "DC":
return "Batman";
case "Marvel":
return "Wolverine";
default:
return "Spiderman"
}
}
// literal
function hero(x = "default") {
const herosMap = {
"DC": "Batman",
"Marvel" : "Wolverine",
"default": "Spiderman"
}
return herosMap[x];
}
Yes yes, I see how you look at the second, it is smaller, and there is this nice trick with default value, what a sexy thing. But, sexy should be your girlfriend/boyfriend, and not exactly your code. Smaller doesn't mean better 😂, when you look at both for sure you need a little more time for the second. Admit it!
These examples are isomorphic, there is no winner here. I don't see any issue in using one or another.
Switch statements are bad in OOP code
Yes, probably better will be your StrategyFactoryProducerPattern. But to be serious, there can be issues with OOP principles about polymorphism and switch. However do you write OOP code? If not those arguments are not a thing.
We can make some FP wrapper to never look at switch again
Why to use switch if you can function? Below example from the Eliminate the switch statement for better code article. You can make such:
const switchcase = cases => defaultCase => key =>
cases.hasOwnProperty(key) ? cases[key] : defaultCase
const counter = (state = 0, action) =>
switchcaseF({
'INCREMENT': () => state + 1,
'DECREMENT': () => state -1
})(state)(action.type)
than simple
const counter = (state = 0, action) => {
switch (action.type) {
case 'INCREMENT':
return state + 1
case 'DECREMENT':
return state - 1
default:
return state
}
}
Yes but first has no {, no explicit return and there is some partial application, notice how sexy this function is called twice at the end (state)(action.type). If this argument works for you, go sleep and read this code again, and I've already said who should be sexy 😉
Switch statement is ok
There is nothing wrong with switch statement, we can use it without the break keyword, it's very explicit and clean to see all possible branches inside the switch. If you think it's not functional programming if we use switch, then look at pure functional languages which have pattern matching. Take a look at below Elm code example:
hero x = case x of
DC -> "Batman"
Marvel -> "Wolverine"
_ -> "Spiderman"
Yes, it is functional programming, and yes case of is expression, but it doesn't mean you should deprecate switch, use it, make it return in every case. Don't complicate things which should be simple.
Thank you.
Posted on May 8, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.