November 26, 20269 minutes
An insurance app where every Policy needs a matching Pricer, until someone forgets one. Learn to spot families that grow in lockstep, merge them, and tell them apart from Bridge.
An insurance app sells policies. There’s a policy for cars, one for homes, and one for travel. Next to them lives a second family: a pricer for each policy, a car pricer, a home pricer and a travel pricer. Look at the names: the prefixes match one to one. Every policy has exactly one pricer. Now the team adds pet insurance. Someone writes the pet policy, and forgets the pet pricer. So pet quotes quietly fall back to a default price. Nothing crashes, and nobody notices until a customer complains. That’s a smell called Parallel Inheritance Hierarchies: every time one family grows, the other must grow too.
Prefer to watch? The narrated video covers the same lesson (about 8 minutes). The article follows it section by section, with diagrams and a little extra depth.
Here is the app before and after the fix this lesson builds.
Think of a building where every new door needs a matching key. You fit the door, and the locksmith cuts its key. Door two, key two. Door three, key three. The two lists grow together, one for one. Forget one key, and that door is useless. Nothing looks broken until someone tries to open it.
Our pet policy is the new door. The pet pricer is the missing key. The danger is the rule you have to remember: a new door always means a new key. A rule that lives only in people’s heads will get broken.
A hierarchy is a family of classes under one parent. This smell is two families that must grow in lockstep.
Here are the policies. A small interface, and a record for each kind.
interface Policy {
double value();
}
record CarPolicy(double value) implements Policy { }
record HomePolicy(double value) implements Policy { }
record TravelPolicy(double value) implements Policy { }Here are the pricers. An interface with one method, and a class for each policy. Each pricer is tiny: it reads the policy’s value and multiplies it by a rate.
interface PolicyPricer {
double price(Policy p);
}
class CarPricer implements PolicyPricer {
public double price(Policy p) { return p.value() * 0.04; }
}
class HomePricer implements PolicyPricer {
public double price(Policy p) { return p.value() * 0.01; }
}
class TravelPricer implements PolicyPricer {
public double price(Policy p) { return p.value() * 0.10; }
}The quoter finds the right pricer in a map, keyed by the policy’s class. And if the map has no entry, it falls back to a default rate. That’s the pet bug, waiting to happen.
class Quoter {
private static final Map<Class<?>, PolicyPricer> PRICERS = Map.of(
CarPolicy.class, new CarPricer(),
HomePolicy.class, new HomePricer(),
TravelPolicy.class, new TravelPricer());
double quote(Policy p) {
PolicyPricer pricer = PRICERS.get(p.getClass());
if (pricer == null) {
return p.value() * 0.05;
}
return pricer.price(p);
}
}(Imports are left out of the snippets; the rates and names are invented examples.)
Honestly, it’s reasonable code. Pricing rules stay out of the policy data, and each pricer is easy to test.
Now the app grows. Pet insurance is next, so someone adds the pet policy.
record PetPolicy(double value) implements Policy { }That compiles, and the tests pass. Nothing says a pricer is missing. The quoter finds no pet pricer, and quietly charges the default rate. Every pet customer is priced by accident.
A correct change touches three places: a new policy, a new pricer, and a map entry. Nothing connects them except a naming habit, so the compiler can’t help. The rule lives in people’s heads.
It’s a special case of Shotgun Surgery: one change, edits in many places. Here the places sit in two families at once.
add CarPolicy, CarPricer, add HomePolicy, HomePricer) are one family in disguise.The Code Smells Reference says the cause is often unconscious: when you design one hierarchy, you build matching ones for the objects it uses. It also adds that the arrangement is manageable while the hierarchies are small, and gets harder to maintain as classes multiply.
Two hierarchies that must grow in lockstep are really one concept, split in two. A policy and its pricer are not two things: pricing is part of what a policy is.
So make one family refer to the other. The pricer should hold its policy, and then the pricer can collapse into it. In practice, move the pricing methods and fields into each policy, and delete the pricer family.
If two families grow in lockstep, they’re one family.
The recipe has two steps (refactoring.guru describes it that way). First, make one family refer to the other. Our pricers already take a policy, so that step is done. Second, move the methods and fields. Fowler’s catalog calls it Move Function, also known as Move Method, and the Code Smells Reference pairs it with Move Field.
Start with the policy interface. It gets a price method. Then each pricer’s body moves into its
policy: the car rate goes into CarPolicy, and the home rate into HomePolicy.
interface Policy {
double value();
double price();
}
record CarPolicy(double value) implements Policy {
public double price() { return value * 0.04; }
}
record HomePolicy(double value) implements Policy {
public double price() { return value * 0.01; }
}
record TravelPolicy(double value) implements Policy {
public double price() { return value * 0.10; }
}Now the pricer family is empty, so delete it. The map goes too, and callers just ask the policy for its price.
double quote = new Quoter().quote(policy);double quote = policy.price();Now replay the pet policy. We write the pet record, and implement the interface:
record PetPolicy(double value) implements Policy { }If we forget the price method, the code does not compile: a class that isn’t abstract must
implement every abstract method of its interfaces (Java Language Specification, section 8.1.5, and
records may implement interfaces). The compiler now holds the rule that used to live in people’s
heads. So we write the price, and the pet quote is correct by construction. There is no default
branch left to fall into.
record PetPolicy(double value) implements Policy {
public double price() { return value * 0.03; }
}One family, one edit, and the pricing sits right next to the data it prices. The cost is that policies now know about pricing. That is fine, because pricing is part of a policy.
Here’s a second shape. A game has vampires and ghouls, and each needs its own weapon class. But look inside the weapons: they differ only in a name and a damage number. That is data, not behavior. So the second family doesn’t need subclasses at all. One weapon record with fields will do, which is what the course page does with its enemy and weapon example.
record Weapon(String name, int damage) { }
record Enemy(String name, Weapon weapon) { }Ask of each pair whether they differ in behavior or in data. Behavior moves into the family you keep. Data becomes fields.
Now an important question. Bridge also has two hierarchies. So is every pair of hierarchies a smell?
No. Look at how they vary. In Bridge, a notification is an alert, a reminder or a digest, and it goes out by email, text or push. Any kind can use any channel. An alert by text, a digest by push, a reminder by email. The two sides vary independently.
In our insurance app, a car policy has exactly one pricer, the car pricer. Never the home one. That’s the test. If any item on the left can pair with any item on the right, it’s Bridge. If each has exactly one partner, it’s lockstep.
Lockstep is the smell. Bridge turns a multiplication into an addition (three kinds and three channels are three plus three classes, not nine), while lockstep only doubles the work: every class twice. A handy question: when I add a new item, must I add one specific partner? If yes, it’s lockstep.
When should you leave parallel hierarchies alone? When the second family must live in a different layer. Say each policy has a row mapper for the database. The domain must not depend on database code, so the policy can’t own its mapper. The Code Smells Reference agrees that the pairing is sometimes acceptable, and refactoring.guru calls it the lesser evil when removing it would make the code uglier.
Then accept the pairing, and guard it with a test. Make the policy interface sealed, and ask for
its permitted subclasses (Class.getPermittedSubclasses(), Java 17):
@Test
void everyPolicyKindHasAMapper() {
for (Class<?> kind : Policy.class.getPermittedSubclasses()) {
assertTrue(PolicyMappers.MAPPERS.containsKey(kind), kind.getSimpleName());
}
}The test fails the moment a new policy has no mapper. The rule stops living in people’s heads.
Two hierarchies that must grow in lockstep are one concept, split in two. Spot it by matching prefixes and paired edits. Fix it with Move Method and Move Field, then delete the second family. It isn’t Bridge, where any item pairs with any other, and it’s a special case of Shotgun Surgery.
Checklist:
If two families grow in lockstep, they’re one family.