POLL: Which do you prefer? Switch/case, if/else, dispatch table?


#1

When I first started developing, the only conditional I used was if/else. A few years later, I learned about switch/case and started using it for every conditional that could be done with switch/case, falling back to if/else for situations where switch/case couldn’t be used.

About four years ago, I learned about dispatch tables and I’m going bonkers with it. I would be using dispatch tables in my professional work, but no one else here wants to use them, so I stick to switch/case and if/else just because no one else wants to know about them and I have to keep my code so that anyone can work on it. (heavy sigh).

What is your preference?

  • if/else
  • switch/case
  • dispatch table

0 voters

V/r,

^ _ ^


#2

I use switch where ever there are more than two possibilities, and the situation fits it. I just find it neater to read, and less to type. Otherwise, I use if/else (for two possibilities, I just find switch a bit of overkill - just my personal opinion). I have never heard of dispatch table (going to go research it now), but I’m not that advanced in JS - I lean towards back-end coding, but working hard on building the JS skillset.

Edit:

I just looked up dispatch tables, and definitely will try them in a practical context. They seem to be easy to read and easy to maintain.


#3

I love the fact that dispatch tables are not conditionals, but can be used similar to a switch/case. Idea being that accessing an objects property is faster than comparing strings.

V/r,

^ _ ^


#4

BTW… four years ago, when I first learned about dispatch tables, I had posted an example on SP.

V/r,

^ _ ^


#5

It depends on what you want to do. A switch / case makes your code more readable if you have one expression you want to test against a number of other expressions using a strict equality check – typically enum-like sets of strings, such as

switch (event.type) {
  case 'click':
    // ...
  case 'mousmove':
    // ...
}

If however you only want to test if an expression is truthy and maybe do something else otherwise, writing out all those (possibly nested) switch/ cases would make your code very verbose and actually much harder to read.

As for dispatch tables, these can be an elegant alternative to a switch / case but not exactly to if / else… compare

if (x > 0) {
  console.log(x + ' is greater than zero')
} else {
  console.log(x + ' is not greater than zero')
}

vs.

;({
  true: () => console.log(x + ' is greater than zero'),
  false: () => console.log(x + ' is not greater than zero')
}[x > 0]())

The latter variant may be a good example of code obfuscation, but if you want to keep your code readable to others better stick to if / else.

Edit: ah sorry this was not meant as a reply to you @WebMachine but to @WolfShade’s OP…


#6

Maybe it’s just me, but your last example was perfectly readable, to me. (shrug) Six and a half of one, half a baker’s dozen of the other. :slight_smile:

And I do realize that any efficiency gain from the dispatch table may be negligible, depending upon case use, but I do like to think that every little bit helps.

V/r,

^ _ ^


#7

I’d agree with @WebMachine - if/else for a question which has two outcomes, or switch/case where there are multiples, regardless of whether I’m in JS (which I’m not, very often) or PHP or Basic or C. I have a dislike for “elseif”.


#8

8 votes, so far, and switch/case is neck and neck with dispatch table. :thinking:

V/r,

^ _ ^


#9

Actually what I use most is none of the above, but only if with early returns.

Just a series of if statements and when one matches, execute what needs to happen and return from the function. That way the code is real easy to read and there is no need for else.


#10

@WolfShade have you considered chained ternary operators / short circuit evaluation BTW?

const doSomething = value =>
  value === 'foo'
  ? console.log('value is "foo"')

  : value === 'bar'
  ? console.log('value is "bar"')

  : value > 42
  && console.log('value is greater than 42')

// quickly hides behind his desk


#11

Heh…

It’s my understanding that if/else will evaluate the code of every instance, utilising only the code that fits (I know for a fact that it works this way in ColdFusion; not positive about JS), and that switch/case will drill down to the one that matches, execute that code, and go on making it slightly more efficient.

But it’s also my understanding that string comparisons in any form are slower than accessing an object property, and this is what makes dispatch tables slightly more efficient than switch/case. Which is one of the reasons I like dispatch tables, so much.

If anyone thinks that my assessment is in error, I am open to discussion on the subject. References appreciated.

V/r,

^ _ ^


#12

Here’s a recent good article with 5 Tips to Write Better Conditionals in JavaScript

There is also mentioned a good article about Replacing switch statements with Object literals