StackUnderflow
StackUnderflow

Reputation: 25378

Is Switch (Case) always wrong?

Are there instances where switch(case) is is a good design choice (except for simplicity) over strategy or similar patterns...

Upvotes: 14

Views: 5002

Answers (8)

Mike A
Mike A

Reputation: 1305

My view is that switch is always wrong:

The body of a case is code and is behaviour, therefore, the thing in the case (the 'value') has a behavioural type, therefore, polymorphism would be a better choice.

This implies that values are in fact types, e.g. the number 1 is a type of everything that equals 1 in some way. All that remains is for us to map 1-ness to the behaviour for our specific case, and we have polymorphism with all those other types (a Good Thing).

This is easier done in some languages than others, unfortunately, most languages in common use are pretty awful, so the path of least resistance is the wrong one, and people end up writing switches or if statements (same thing).

Upvotes: -4

interstar
interstar

Reputation: 27206

Use Switches when you're testing on values of primitives. (ie. integers or characters).

Use polymorphism when you are choosing between different types.

Examples : Testing whether a character the user has entered is one of 'a', 'b' or 'c' is a job for a switch.

Testing whether the object you're dealing with is a Dog or Cat is a job for polymorphic dispatch.

In many languages, if you have more complicated values you may not be able to use Switch anyway.

Upvotes: 30

javelinBCD
javelinBCD

Reputation: 1931

No, the switch statement is probably only a good design choice in simple situations.

Once you are passed a simple situation switch statements become very painful to keep updating and maintaining. This is part of the reason design patterns came about.

Upvotes: -1

James Curran
James Curran

Reputation: 103545

First of all, Simplicity often is a good design choice.

I never understood this bias against switch/case. Yes, it can be abused, but that, so can just about every other programming construct.

Switching on a type is usually wrong and probably should be replaced by polymorphism. Switching on other things is usually OK.

Upvotes: 18

lacker
lacker

Reputation: 5550

Yes, definitely. Many times your switch is only relevant to a very small part of your overall logic and it would be a mistake to create whole new classes just for this minor effect.

For example, let's say you have a database of words, the user input another word, and you want to find that word in the database but include possible plurals. You might write something like (C++)


vector<string> possible_forms;
possible_forms.push_back(word);
char last_letter = word[word.size() - 1];
switch (last_letter) {
  case 's':
  case 'i':
  case 'z':
    possible_forms.push_back(word + "es");
    break;
  case 'y':
    possible_forms.push_back(word.substr(0, word.size() - 1) + "ies");
    break;
  default:
    possible_forms.push_back(word + "s");
}

Doing this with strategies would be overkill.

Upvotes: 5

OscarRyz
OscarRyz

Reputation: 199274

The "strategies" could be created with a switch.

That could be the starting point and from there let the polymorphism do the job.

Other that comes to mind need for extra speed at the cost of flexibility. There are cases.

Upvotes: 0

Ray Tayek
Ray Tayek

Reputation: 10003

it's usually ok, as long as you only have one switch in one place. when you have more than one (or many), then it's time too consider alternatives.

Upvotes: 1

user36457
user36457

Reputation:

For one, readability.

Upvotes: 5

Related Questions