November 26, 20268 minutes
One Invoice class, three teams, three merge conflicts in one sprint. Learn to spot a class with many reasons to change, split it with Extract Class, and know when to leave it.
It’s the last week of the sprint, and one file keeps changing: the Invoice class. On Monday,
finance changes the tax rule. On Wednesday, design changes the layout of the PDF. On Friday, the
platform team switches the email provider. Three teams, three unrelated reasons, and three merge
conflicts, all in one file. That’s a smell called Divergent Change: one class, changed for many
unrelated reasons.
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 that sprint as a picture. Nobody edited anything they shouldn’t have; the file simply has three owners.
Picture the whiteboard in a shared kitchen. Everyone in the house writes on it. One housemate works out who owes how much rent, another keeps the chore rota, and a third keeps the shopping list. Nobody is doing anything wrong. But every edit smudges the others, and one person’s list gets wiped by accident.
That board is our Invoice class. The sections are tax, layout and delivery, and each one belongs
to a different team. Each team is a reason to change, and this class has three of them.
Here’s the invoice class. It holds the lines, the customer’s email and the region. Then it does four jobs: it adds up the subtotal, works out the tax, draws the PDF, and sends the email.
record Invoice(List<Line> lines, String customerEmail,
String region) {
double subtotal() {
return lines.stream().mapToDouble(Line::price).sum();
}
double tax() {
boolean eu = region.equals("EU");
double rate = eu ? 0.20 : 0.08;
return subtotal() * rate;
}
byte[] toPdf() {
Pdf pdf = new Pdf();
pdf.heading("Invoice");
for (Line l : lines) {
pdf.row(l.name(), l.price());
}
pdf.row("Tax", tax());
return pdf.bytes();
}
void email() {
byte[] pdf = toPdf();
MailClient.connect("smtp.acme.test");
MailClient.send(customerEmail, "Invoice", pdf);
}
}(Line is a small record with a name and a price. Pdf and MailClient stand for a PDF library
and a mail library; they’re placeholders, not real APIs.)
Honestly, that’s a natural place to start. An invoice has tax, a layout and a delivery, and the code reads top to bottom. Everything about invoices is in one place, and for a while that’s convenient.
Now the sprint begins. On Monday, finance raises the European tax rate: an edit to the eu and
rate lines. On Wednesday, design adds a logo to the PDF: an edit inside toPdf. On Friday, the
platform team moves to a new mail server: an edit inside email. Three edits, three branches, one
file. Each reviewer must read the other two teams’ code to be sure nothing broke.
And the parts are tangled. The PDF method calls tax(), and the email method calls toPdf(). So a
change to the layout can break the tax line. Nobody owns the whole class, and every edit feels
risky.
git log --oneline -- Invoice.java on that one file and read the messages.Don’t group code by the thing it’s about. Group it by the reason it changes. Notice that the noun is the same in all three jobs: it’s always “invoice”. Only the reasons differ.
For each method, ask who would request a change to it. If the answers differ, the methods don’t belong together. Think of a reason as a person or a team who can ask for an edit, not as a single method. Robert Martin’s single responsibility principle says the same: a class should have one reason to change.
One class, one reason to change.
The refactoring is called Extract Class: take the part that changes for one reason, and give it its own class. Start with tax. It needs only the region and the subtotal, so the new tax calculator takes an invoice, and the invoice keeps its lines and its subtotal.
class TaxCalculator {
double tax(Invoice inv) {
boolean eu = inv.region().equals("EU");
double rate = eu ? 0.20 : 0.08;
return inv.subtotal() * rate;
}
}record Invoice(List<Line> lines, String customerEmail,
String region) {
double subtotal() {
return lines.stream().mapToDouble(Line::price).sum();
}
}Next, the layout. The invoice PDF renderer draws the page, and asks the tax calculator for the tax line.
record InvoicePdfRenderer(TaxCalculator taxes) {
byte[] render(Invoice inv) {
Pdf pdf = new Pdf();
pdf.heading("Invoice");
for (Line l : inv.lines()) {
pdf.row(l.name(), l.price());
}
pdf.row("Tax", taxes.tax(inv));
return pdf.bytes();
}
}Last, delivery. The invoice mailer asks the renderer for the PDF, and sends it.
record InvoiceMailer(InvoicePdfRenderer renderer) {
void send(Invoice inv) {
String to = inv.customerEmail();
byte[] pdf = renderer.render(inv);
MailClient.connect("smtp.acme.test");
MailClient.send(to, "Invoice", pdf);
}
}Invoice is now just data and its core behavior. Each other class has one reason to change, and
one team that owns it.
When the concerns run in a sequence, like tax, then layout, then delivery, Fowler also offers Split Phase: separate the steps with a clear data structure between them. When calls go back and forth between two concerns, he suggests Move Function; when one function mixes both kinds of work, Extract Function first. For classes, Extract Class is the usual tool. The refactoring.guru page also lists Extract Superclass and Extract Subclass for classes that share behavior.
Now replay the sprint. On Monday, the tax rule changes, and only the tax calculator is edited. On Wednesday, the layout changes, and only the renderer is edited. On Friday, the provider changes, and only the mailer is edited. Three teams, three files, and no conflicts. And you can test the tax rule without drawing a single PDF.
There is a price. Three classes mean more files and a few more constructor arguments. That’s fine when each class has its own owner. Name each new class after its reason; if you can’t name the reason, you may have split in the wrong place.
Here’s a smaller case. A reminder has a text and a due time, and two methods.
record Reminder(String text, LocalDateTime due) {
boolean isDue(LocalDateTime now) {
return !now.isBefore(due);
}
void notifyUser() {
PushService.send("Reminder: " + text);
}
}isDue follows the product rules, like snoozing or quiet hours. notifyUser follows the platform:
push messages today, maybe texts tomorrow. That’s a tiny class with two reasons to change. So the
size isn’t the signal; the reasons are. In the history, it shows up the same way: one commit about
snoozing, another about push messages.
Same move: Extract Class. The reminder keeps its data and its rule, and the reminder notifier sends the message.
record Reminder(String text, LocalDateTime due) {
boolean isDue(LocalDateTime now) {
return !now.isBefore(due);
}
}
class ReminderNotifier {
void send(Reminder r) {
PushService.send("Reminder: " + r.text());
}
}Divergent change has a mirror image: Shotgun Surgery. In divergent change, one class is changed for many reasons. In shotgun surgery, one reason forces changes in many classes.
Back at the whiteboard, divergent change is three housemates editing one board. Shotgun surgery is one fact, like a housemate moving out, that has to be fixed on every board. The fixes mirror each other too: divergent change splits a class apart with Extract Class, while shotgun surgery pulls scattered code together with Move Method and Inline Class.
Two myths to drop.
So when should you leave it alone?
Fowler adds that the boundaries often only show up after several changes. So let the history tell you where to split.
Divergent change is one class that keeps changing for unrelated reasons. Spot it in the history, the teams and the fields. Fix it with Extract Class: one class for each reason. It mirrors shotgun surgery, and it isn’t the same as a large class.
Checklist:
One class, one reason to change.