Refactoring Replace Type Code with Subclasses
What is type code? Type code occurs when, instead of a separate data type, you have a set of numbers or strings that form a list of allowable values for some entity. Often these specific numbers and strings are given understandable names via constants, which is the reason for why such type code is encountered so much.
ProblemYou have a coded type that directly affects program behavior (values of this field trigger various code in conditionals).
SolutionCreate subclasses for each value of the coded type. Then extract the relevant behaviors from the original class to these subclasses. Replace the control flow code with polymorphism.
This refactoring technique is a more complicated twist on Replace Type Code with Class.
As in the first refactoring method, you have a set of simple values that constitute all the allowed values for a field. Although these values are often specified as constants and have understandable names, their use makes your code very error-prone since they are still primitives in effect. For example, you have a method that accepts one of these values in the parameters. At a certain moment, instead of the constant
USER_TYPE_ADMIN with the value
"ADMIN", the method receives the same string in lower case (
"admin"), which will cause execution of something else that the author (you) did not intend.
Here we are dealing with control flow code such as the conditionals
?:. In other words, fields with coded values (such as
$user->type === self::USER_TYPE_ADMIN) are used inside the conditions of these operators. If we were to use Replace Type Code with Class here, all these control flow constructions would be best moved to a class responsible for the data type. Ultimately, this would of course create a type class very similar to the original one, with the same problems as well.
Delete the control flow code. Instead of a bulky
switchin the original class, move the code to appropriate subclasses. This improves adherence to the Single Responsibility Principle and makes the program more readable in general.
If you need to add a new value for a coded type, all you need to do is add a new subclass without touching the existing code (cf. the Open/Closed Principle).
By replacing type code with classes, we pave the way for type hinting for methods and fields at the level of the programming language. This would not be possible using simple numeric or string values contained in a coded type.
When Not to Use
This technique is not applicable if you already have a class hierarchy. You cannot create a dual hierarchy via inheritance in object-oriented programming. Still, you can replace type code via composition instead of inheritance. To do so, use Replace Type Code with State/Strategy.
If the values of type code can change after an object is created, avoid this technique. We would have to somehow replace the class of the object itself on the fly, which is not possible. Still, an alternative in this case too would be Replace Type Code with State/Strategy.
How to Refactor
Use Self Encapsulate Field to create a getter for the field that contains type code.
Make the superclass constructor private. Create a static factory method with the same parameters as the superclass constructor. It must contain the parameter that will take the starting values of the coded type. Depending on this parameter, the factory method will create objects of various subclasses. To do so, in its code you must create a large conditional but, at least, it will be the only one when it is truly necessary; otherwise, subclasses and polymorphism will do.
Create a unique subclass for each value of the coded type. In it, redefine the getter of the coded type so that it returns the corresponding value of the coded type.
Delete the field with type code from the superclass. Make its getter abstract.
When everything possible has been moved, use Replace Conditional with Polymorphism in order to get rid of conditions that use the type code once and for all.
Tired of reading?
No wonder, there are 7 hours worth of the text on this website.
Try out something different. We've just launched the interactive learning course on refactoring. It has more content and much more fun than a boring text.Learn more...
Everything is quite simple. This example is like video (but much cooler):
- After pressing the Play button, you will see floating messages, which will guide you through the example. You can proceed by clicking on them.
- You can fast-forward or return to previous steps by pressing arrow buttons on the left.
- Also, you can take a look at the code diff window, which will show what has changed in code during the example ()
- To check the code for errors, you can press "Compile and test" button ()