Signs and Symptoms
You have a complex
switch operator or sequence of
Reasons for the Problem
Relatively rare use of
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.
switchand put it in the right class, you may need Extract Method and then Move Method.
switchis based on type code, such as when the program's runtime mode is switched, use Replace Type Code with Subclasses or Replace Type Code with State/Strategy.
After specifying the inheritance structure, use Replace Conditional with Polymorphism.
If there aren't too many conditions in the operator and they all call same method with different parameters, polymorphism will be superfluous. If this case, you can break that method into multiple smaller methods with Replace Parameter with Explicit Methods and change the
If one of the conditional options is
null, use Introduce Null Object.
- Improved code organization.
When to Ignore
switchoperator performs simple actions, there's no reason to make code changes.
switchoperators are used by factory design patterns (Factory Method or Abstract Factory) to select a created class.