7

This question already has an answer here:

I am developing a game in JavaScript where you start with a user input, stored in the variable "controller". The options for the user consists of start to start the game or about to learn about the game. I was going to use the following code

if(controller === "start"){
    // Game code sitting here
}
else if(controller === "about"){
    // All about the Game
}
else{
    // Tells the user again to type start to start or about to learn about the game
}

when I realized that I should maybe use switch cases instead...

So, my question is when to use switch cases instead of if statements and which I should use in this case. I would also like to know if you think I should store my game code in a function and call it if controller is equal to "start" or just have it sitting inside the if statement/switch case as it is atm?

marked as duplicate by gnat, Bart van Ingen Schenau, GlenH7, user40980, Robert Harvey Dec 23 '13 at 23:26

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • You could also use an object as a lookup table var controllers = {"start": function(){...}, "about": function(){...}} – Andreas Dec 23 '13 at 8:33
10

When to use switch instead of if statements?

Use switch instead of if when:

  1. You are comparing multiple possible conditions of an expression and the expression itself is non-trivial.
  2. You have multiple values that may require the same code.
  3. You have some values that will require essentially all of another value's execution, plus only a few statements.

Use if instead of switch when:

  1. You want to test for the truthiness of an expression.
  2. You only have a single affirmative test.
  3. You need to evaluate different expressions for each branch.

which I should use in this case?

Whichever you are more comfortable with. For a state engine evaluation, you would gain a slight bit of flexibility with a switch statement, in that you could slightly more easily add synonyms latter on. But the difference is too minor to matter.

Should [I] store my game code in [seperate] functions, or just have it sitting inside the if statement/switch case as it is atm?

You should definitely move the inner code of each branch to distinct functions. You cannot easily unit-test the functionality if it's sitting in the middle of a thousand-line function.

4

Semantics, semantics, semantics.

Personally, in a situation like this, I make a judgement call as to which one will be easier to understand.

What influences the ease of understanding, again, in my opinion, is maybe a combination of several factors:

  1. Collectively exhaustive - Is there a specific way to handle each legal value? If so, a switch may be in order.
  2. Dependency / mutual exclusive - Do any conditions depend on each other? If so, an if statement may be more flexible.
  3. More than 2 cases - switch/case has some cognitive overhead for me, so I would be annoyed to see a switch/case for true/false. Possibly because if (x) { } else { } is leaner.
  4. How similar are the actions? Do they fall in the same domain? If one case sets or returns a variable, while another case executes a function that mutates some state and another case prints / outputs a piece of information, I would not make that a case because the cases are not related at all. If they all, however, switch on a menu selection and execute a different function as the next step, that is a good switch because all cases choose a "next step".

The first two, collectively exhaustive and mutually exclusive, come to me by thinking about a physical switch. Let's say I have a switch that can be in 5 states. To really be a switch, it can only be in one state at any given time. It can never not have a state and it cannot co-exist with another state. In software, this is like saying that for any input, at least one of the case statements must match (including the default)..

Again, this is my personal preference. I do not know if there is any official guideline on this.

0

Cases fit this operation nicely. You could also instantiate a singleton dictionary and then populate it with all the functions that you'd need the controller to perform and then simply go through the dictionary each time the controller is activated.

Using if/then would prove problematic since it'd be much hard to maintain than a case statement or a dictionary.

Not the answer you're looking for? Browse other questions tagged or ask your own question.