0

I have a function that takes an input and does a switch case based on a specific input property. So the function itself is pretty simple, as you can see below

function handleMessage(message) {
  switch (message.type) {
    case "a":
      a(message);
      break;
    case "b":
      b(message);
      break;
    case "c":
      c(message);
      break;
    case "d":
      d(message);
      break;
    case "e":
      e(message);
      break;
  }
}

But if I check the complexity for this functions I get 12, which VSCode says is too much (according to some recommendation found online the highest value for a function should be 10). As long as I add cases to the switch statement the complexity will of course increase, but the point is: is anything I can do to decrease this value? Or should I just deal with it?

Jimi
  • 1,605
  • 1
  • 16
  • 33
  • I've seen it before but can't really find an answer. An interesting approach seems to do something like this: a dictionary where the keys are the switch cases and the values are the functions handled by the case, but at that point I might loose some readability and maintainability, which are also important – Jimi Jan 21 '20 at 15:03
  • So, I've implemented that approach and indeed it reduced the complexity by a lot (it is now 2). But what if you want to handle the `default` case inside the dictionary (if even possible)? – Jimi Jan 21 '20 at 15:29

0 Answers0