In my recent post I questioned the use of a switch instead of an if else statement. This gave me the idea of an recurring item for dev.to: Commit or Vomit! Would you commit this code or not.
It's going to be code snippets that are going to be evaluated here. Not posts because I don't want people to feel/be judged by this, only code!
So this first β||π€’ is from the post that started it all.

If (all) else is complicated, switch to switch?
π€π₯ Jasper de Jager γ» Apr 1 '21
switch(true){
case userMissedAppointment:
return 'nope';
case userHasAngularExperience:
case userHasReactExperience:
case userHasVueExperience && userCanStartInstantly:
return 'hire';
default:
return 'maybe'
}
This is just an example but the question is about the switch(true). What do you think? β||π€’
β€: Commit
π·: Vomit (we all know unicorns don't vomit)
π¦: Like your post please continue this series!
Looking forward to your reactions! π
Oldest comments (50)
This was a tough one for me, I like the style, but if/else is easier to read and comprehend. so for me it's a π€’
This could easily have been an if/elseif/else.
You could have done:
true, this is a short example. Apart from this example would you never commit a switch(true)?
Nope π€’
I'm not sure it's because of the editor, but I would suggest to use more lines for more readability and easier comprehension.
For me this already reduces the cognitive load a lot quickly see what can happen.
Actually I typed my comment on mobile, so I didn't format it
Good point for a next Commit or Vomit! What do you think about switch(true) in general?
good to mention: the example was updated after this reply
and because this is the first:
Please leave comments if you see improvements for this item π
Looking forward to the next one.
I'm on mobile so I didn't really format anything
It was sufficient to make your point π no worries.
I'd really like the discussion to be about the switch(true) and not about the code in comments. The code posted gives a good idea about why he voted vomit. It is not the code up for discussion.
You're right. Switching true just adds a few extra lines and makes us momentarily think "is it a typo? Why would you switch true". At least I did when I first saw a switch(true)
I'd say probably vomit, but I'm sure there are cases where this way is just easier to read than if/else/return early/etc. Hard to say with placeholder-var-names.
Yes I agree, something to take into account for the next one in the series!
Thanks for the feedback π
switch(true){
case userDoesntHaveWorkPermit:
console.log('nope');
break;
case userHasReactExperience:
console.log('Hire!');
break;
case userHasVueExperience
&& userCanStartInstantly:
console.log("Hire, if they can start instantly");
break;
default:
console.log('maybe');
}
More of an Angular fan myself so userHasAngularExperience is going to be added π but much better example, thnx!
Snippet was updated!
no is not readable
I can't believe some people put hearts
That's the fun of this item π
It shows it's important to keep an open mind!
I liked this post, not because I think
switch (true)
is a good paradigm (it isn't), but because the post itself is interesting.The author isn't saying "you should use
switch (true)
in your code", they're saying "let's have a discussion about whetherswitch (true)
is acceptable". IMO, that's a useful discussion!I think the cleanest and most expressive is still this
Your function behaves slightly different than the example, bit this just might prove your point of the switch not being readable enough π
The difference is that in the switch an angular experienced used doesn't have to start immediately.
You are definitely right :D Well in that case I wouldn't mind the right if else combo either