I'm excited, we get to add a factory pattern!
Data Clump Code Smell: the situation in which several (>3) data fields are routinely grouped together
-
The presence of this code smells means that you are missing a concept. The code can get muddy, duplicated and accidentally mismanaged if not careful
-
The value of removing data clumps is great
In the case of 99 bottles, notice how quantity
and container
are always grouped together? That's an example of a data clump. Move these two to their own method and call them correspondingly
Switch cases are sometimes a code smell and their refactoring recipes are
-
Replace Conditional with State/Strategy
Removes conditionals by dispering their branches into new, smaller objects, one of which is selected and plugged back in at runtime. Doesn't use inheritance
-
Replace Conditional with Polymorphism
Removes conditionals by creating one class to hold the default of the conditionals (false branches) and adding subclasses for each specialization (true branches). It chooses one of the objects to plug back in at runtime. Uses inheritance
The best way to figure out which refactoring recipe is right is to try them all, evaluate, pick one and go with it. Practice builds intuition
Extracts the primitive obsession with 1
and 0
into their own BottleNumber0
class and a default class. Moves the logic for deciding which class to return into a factory.
Clever shortcuts are a false economy. Invest in code that tells the truth.