ConstructiCat Logo
CodeBust.
Browse section ▾

Switch Statements.

You have a complex switch operator or sequence of if statements.

##Signs and Symptoms

You have a complex switch operator or sequence of if statements.

double pay(Employee e) {
  switch (e.type) {
    case "engineer": return e.salary;
    case "manager": return e.salary + e.bonus;
    case "intern": return e.salary / 2;
    default: throw new IllegalArgumentException(e.type);
  }
}

##Reasons for the Problem

Relatively rare use of switch and case operators is one of the hallmarks of object-oriented code. Often code for a single switch can be scattered in different places in the program. When a new condition is added, you have to find all the switch code and modify it.

As a rule of thumb, when you see switch you should think of polymorphism.

##Treatment

##Payoff

  • Improved code organization.