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...
For further actions, you may consider blocking this person and/or reporting abuse
The best thing about switch statements is that you're not limited to strings.
Nice. Thanks for sharing
I am in the bandwagon of object literals over switches, I know it doesn't have a tremendous impact but combining it with some ES8 features like optional chaining and nil colascing makes it even better IMO:
It goes to the point that switch is not expression. Don't get we wrong optional chaining and nulish colascing are great feature, but I don't believe using them as you have shown is a good example. It's just not needed here.
Hahaha I agree completely, specially nil colascing which would easily be replaced with a logical or instead, it’s just a personal preference
Your example with the object literal is overly long:
I have waited for that 😉
I've been in both camps before. I think I went at least 10 years without writing a
switch/case
anywhere. But in the last several years, I've actually come back toswitch
statements. It's not like I use them everywhere - or even, often. But I do use them. IMHO, the "proper" use-case for switches is whenever you are checking one variable against a set of known values. I do not like any use of aswitch
where people are trying to embed logic that goes beyond the basic idea of a switch.Also, although I'm, admittedly, a Certified Redux Hater, it's hard for anyone to love Redux and hate switches. I've even seen Redux fanboys grumble about switches that are written outside of their reducers - but they're perfectly fine using a ton of switches inside their reducers. Such pedantic distinctions make little sense.
Railing against the use of
switch
makes no more sense than railing against awhile
loop. Arewhile
loops usually the "right" choice?? No. But there are absolutely times when awhile
loop is the right choice.Can we all just gather around the campfire and agree that redux is an unmatched headache? 😂 People dump implementation logic of 50 lines in their reducers and the code becomes impossible to follow in large projects.
I'm constantly telling those phrases to Kotlin fans :)
My favourite Javascript
switch
feature is the ability to useswitch(true)
followed by cases with with comparison operations.Looks much cleaner compared to a lot of
if/else
.can just be
unless you need it to be so dynamic (uses user input or is an event handler that lacks types) that your code doesn't know which action to take without user input. if you end up writing:
...you are just converting function names into string names and making your code breaky and complicated and too clever for its own good
Depends on how extreme it is. If i have to spend an extra 10 seconds reading your code to understand something that a switch could tell me in 2 lines, and i have to do that every time i read your code 20 times a day for a year, we are having a meeting 😂 Otherwise, people can do whatever they want for all i care.
Lodash _.cond with lodash/fp version for the win!
Yes you are right, I have wrongly made a title of this paragraph. Changed already
+1, although I would love for pattern matching to land in JavaScript as it's the best of both worlds :)
I love switch statements because they do the thing I read about all the time: "Make readable code." If readable code is virtuous, then switch should not be deprecated.