Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best alternative to an out of control switch statement?

I have inherited a project that has some huge switch statement blocks, with some containing up to 20 cases. What is a good way to rewrite these?

like image 339
Asmussen Avatar asked Jan 04 '09 06:01

Asmussen


People also ask

What is a good substitute for a switch statement?

:) Author has 1.4K answers and 2.3M answer views 5 y Object oriented programming (OOP), that is virtual functions, are a substitute for switch. You can also look into “multiple dispatch” languages like Julia; it doesn’t have a switch statement, and is more general than traditional single-dispatch OOP.

Is it okay to use a switch statement?

0 It depends what the switch statement is doing. If it's matching characters or strings, say in a parser, and you don't have the same set of patterns repeated everywhere in the code, then a switch statement might be ok.

What are the important points about SWITCH CASE statements?

Important Points about Switch Case Statements: 1 The expression provided in the switch should result in a constant value otherwise it would not be valid. ... 2 Duplicate case values are not allowed. 3 The default statement is optional.Even if the switch case statement do not have a default statement, it would run without any problem. More items...

What is switch case in C++?

Switch case statements are a substitute for long if statements that compare a variable to several integral values. The switch statement is a multiway branch statement.


2 Answers

As others have pointed out, it depends on the switch statement. However, in the past I have refactored switch statements by proceeding in the following way. Suppose we have a switch statement like this, with a lot of repeating code

switch(x){
   case 1:
     makeitso("foo");
     globalLog += "foo";
   case 2:
     makeitso("bar");
     globalLog += "bar";
   case 3:
     makeitso("baz");
     globalLog += "baz";
   ...
   default:
      throw("input error");
}

the first thing to do is to recognize the parts that are common (in the real world, this will probably be a little more substantial)

makeitso([some string]);
globalLog += [some string];

and turn that into a function

function transformInput(somestring) {
     makeitso(somestring);
     globalLog += somestring;
}

then for the parts that change in each case, use a hash or an array;

var transformvalues = ["foo", "bar", "baz"];

from here we can do this:

var tvals = ["foo", "bar", "baz" ... ];
function transformInput(somestring) {
     makeitso(somestring);
     globalLog += somestring;
}
var tval = tvals[x];
if(tval!==undefined) {
     transformInput(tval);
} else {
    throw ("invalid input");
} 

And with tvals factored out of the switch statement, it could even be provided externally to expand the number of cases you can handle. Or you could build it dynamically. In the real world, the switch statement will often have special cases, however. I leave that as an excercise for the reader.

like image 51
Breton Avatar answered Oct 21 '22 01:10

Breton


Three suggestions (echoing some already given):

  1. Maybe a switch isn't as bad as you think. It can be ugly if the case blocks are large. Shorten them by extracting the logic into methods.

  2. In an OO language, polymorphism might be the answer, as many have pointed out.

  3. In a functional language, like Javascript, write a function that returns the function you need to run for whatever input. That might use a switch statement itself, or it might use a lookup table.

like image 42
slim Avatar answered Oct 20 '22 23:10

slim