DEV Community

Discussion on: Why switch is better than if-else

Collapse
 
johncip profile image
jmc • Edited

A switch indicates that you are mapping from an input value to a piece of code.

IMO when there's fallthrough, you're not mapping from values to code, but rather values to an offset in code. Like a jump table. I mention it not because I consider it to be all that dangerous but because in those languages it waters down the idea of switch-as-map.

But I agree on the whole. Often pattern matching is what you want (or the repeated application of a predicate, a la condp), and switch isn't always a perfect fit, but where it works it tends to be more explicit than the alternative.