DEV Community

Cover image for βœ”||🀒 Commit or Vomit | Switch(true)
 🐀πŸ₯‡ Jasper de Jager
🐀πŸ₯‡ Jasper de Jager

Posted on • Edited on

βœ”||🀒 Commit or Vomit | Switch(true)

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.


switch(true){
    case userMissedAppointment:
        return 'nope';
    case userHasAngularExperience:
    case userHasReactExperience:
    case userHasVueExperience && userCanStartInstantly:
        return 'hire';
    default:
        return 'maybe'
}
Enter fullscreen mode Exit fullscreen mode

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)

Collapse
 
jmdejager profile image
🐀πŸ₯‡ Jasper de Jager

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 🀒

Collapse
 
siddharthshyniben profile image
Siddharth • Edited

This could easily have been an if/elseif/else.

You could have done:

if(someExpressionA) console.log('yes')
else if ((someExpressionB 
    && someExpressionC) || someExpressionD) console.log('nope');
else console.log('maybe');
Enter fullscreen mode Exit fullscreen mode
Collapse
 
jmdejager profile image
🐀πŸ₯‡ Jasper de Jager

true, this is a short example. Apart from this example would you never commit a switch(true)?

Collapse
 
siddharthshyniben profile image
Siddharth • Edited

Nope 🀒

Collapse
 
basbenik profile image
Bas van Baalen

I'm not sure it's because of the editor, but I would suggest to use more lines for more readability and easier comprehension.

if(someExpressionA) 
  console.log('yes')
else if ((someExpressionB && someExpressionC) || someExpressionD) 
  console.log('nope');
else 
  console.log('maybe');
Enter fullscreen mode Exit fullscreen mode

For me this already reduces the cognitive load a lot quickly see what can happen.

Collapse
 
siddharthshyniben profile image
Siddharth

Actually I typed my comment on mobile, so I didn't format it

Collapse
 
jmdejager profile image
🐀πŸ₯‡ Jasper de Jager

Good point for a next Commit or Vomit! What do you think about switch(true) in general?

Collapse
 
jmdejager profile image
🐀πŸ₯‡ Jasper de Jager

good to mention: the example was updated after this reply

Collapse
 
jmdejager profile image
🐀πŸ₯‡ Jasper de Jager

and because this is the first:
Please leave comments if you see improvements for this item 😎

Collapse
 
siddharthshyniben profile image
Siddharth

Looking forward to the next one.

Collapse
 
siddharthshyniben profile image
Siddharth

I'm on mobile so I didn't really format anything

Thread Thread
 
jmdejager profile image
🐀πŸ₯‡ Jasper de Jager

It was sufficient to make your point 😊 no worries.

Collapse
 
jmdejager profile image
🐀πŸ₯‡ Jasper de Jager

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.

Thread Thread
 
siddharthshyniben profile image
Siddharth • Edited

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)

Collapse
 
niorad profile image
Antonio Radovcic

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.

Collapse
 
jmdejager profile image
🐀πŸ₯‡ Jasper de Jager

Yes I agree, something to take into account for the next one in the series!
Thanks for the feedback 😊

Collapse
 
niorad profile image
Antonio Radovcic

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');
}

Thread Thread
 
jmdejager profile image
🐀πŸ₯‡ Jasper de Jager

More of an Angular fan myself so userHasAngularExperience is going to be added 😎 but much better example, thnx!

Thread Thread
 
jmdejager profile image
🐀πŸ₯‡ Jasper de Jager

Snippet was updated!

Collapse
 
mahyargp profile image
Hossen Eki

no is not readable

Collapse
 
siddharthshyniben profile image
Siddharth

I can't believe some people put hearts

Collapse
 
jmdejager profile image
🐀πŸ₯‡ Jasper de Jager

That's the fun of this item 😊
It shows it's important to keep an open mind!

Collapse
 
cariehl profile image
Cooper Riehl

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 whether switch (true) is acceptable". IMO, that's a useful discussion!

Collapse
 
asdftd profile image
Milebroke • Edited

I think the cleanest and most expressive is still this


const hasFrontendFrameworkExperience = userHasAngularExperience || userHasReactExperience || userHasVueExperience;

if(userMissedAppointment){
    return 'nope';
} else if(hasFrontendFrameworkExperience && userCanStartInstantly){
    return 'hire';
}
return 'maybe';
Enter fullscreen mode Exit fullscreen mode
Collapse
 
jmdejager profile image
🐀πŸ₯‡ Jasper de Jager

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.

Collapse
 
asdftd profile image
Milebroke

You are definitely right :D Well in that case I wouldn't mind the right if else combo either