📕

(EN) switch-case opinions and ideas

2023/09/25に公開

Why it's not used much

Out of all the people I met and talked to, a vast majority actually prefere using the if-else alternative instead of the switch-case.
But there are not many use cases for the switch-case anyways, so why do people still prefere to use the if-case in those very few occasions?

  • while the switch-case is definetely a performance improvement, the if-else isn't that heavy in the first place so the improvement has little to no impact in the overall
  • the switch case doesn't actually make the code much easier to read/write
  • the if-else is something you use everywhere, and people are more accustomed to it
  • apparently (haven't tested it) there are some scenarios in wich the code may be functioning but the tests give issues due to "unoptimized coding"

While it varies depending on the job, as a (mostly) website backend developer there are so few use cases that I always forget how to write it, and end up having to google it each time.

Why is it "difficult to read/write"

Let's see what would be a three option comparison between the two.
Here is the code if you were using an if-else:

if(animal === 'dog') {
  console.log('bark');
} else if(animal === 'cat') {
  console.log('meow');
}else if(animal === 'wolf') {
  console.log('howl');
} else {
  console.log('silence');
}

Here is the code if you were using a switch-case:

switch (animal) {
  case 'dog':
    console.log('barks');
    break;
  case 'cat':
    console.log('meows');
    break;
  case 'wolf':
    console.log('howls');
    break;
  default:
    console.log('silence');
}

As you can see by the example there are two fatal flaws in the switch case:

  • your switch-case has to be indented twice, while the if-else only indents once
  • your switch-case is quite longer in comparison, 9 rows against 13 acutally!

How can we "improve" it?

Lets look at the code bit by bit, and see what we can scrape off to make if more simple.

the switch

switch (animal) {

This is probably the only part of the code that can't be improved, as every part is necessary, and it is quite easy to understand.

the case

case 'dog':

While this section may seem normal there in on thing that bothered me:
Why is there a "case"? Why is it a "switch-case" and not just a "switch"?
No other operator has the "case" in their name, and we can just rely on the : to undertand what we are trying to compare.
Would't something like this work just fine?

'dog':

and if we have multiple cases for a single action we can lump the together like so:

case 'dog':
case 'puppy':

Allowing us to avoid having to use one line for each case:

'dog', 'puppy':

the action

console.log('barks');

The main issue with the action is thatit indents the code twice.
This is unavoidable when there are multiple lines of code, why is that the case for a one-liner?
Lets just forget about the break; for a moment and put the action on the same line as the case.

'dog': console.log('barks');

the break

break;

the break is an essential part of the switch case, and is almost always there.
Personally I believe that having the break should be the default, and not the other way round, so instead of having to add the break every time it would be better to have a way to write when we don't want to break.
But we also don't want the "keep-going" sign to take a whole line by itself, so this is the conslusion I came to:

// when we want to break:
'dog': console.log('barks');
// when we want to fall-through
'dog':> console.log('barks');

This should be intuitive enough, and make it impossible for people to accidentally forget to add the break (which is apparently an occurrence happeninng quite often).

Here is a link to find out more about break and fall-through in a switch case:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch

My version of the "switch"

Here is the final code if we put this all together:

switch (animal) {
  'dog': console.log('barks');
  'cat': console.log('meows');
  'wolf': console.log('howls');
  default: console.log('silence');
}

Depending on the preference adding back the "case" might be a good idea, but I believe it should be allowed to work even without it.

Conclusion

These are just my personal thoughts on what I feel like is a very unpopular operator. If you have your own thoughts and ideas please share!
I tried making a javascript parser, and am planning to try the same for other languages, please feel free to check them out!
https://github.com/TheBlindHawk/JS-switch

(as it's my first time writing a parser the code is probably going to need improvements...)

Discussion