November 26, 20268 minutes
One line prices a seat through five classes, and a small change breaks six screens. Learn to spot Message Chains, fix them with Hide Delegate or Move Method, and tell them from fluent calls.
A box office screen needs the price of a seat. So it reads it straight off the ticket: the ticket’s event, the event’s venue, the venue’s seat map, then a seat, and finally its price. Then the venue team restructures the seat map into sections. One small change, in one class, and six screens stop compiling. The screens did nothing wrong. They just know too much. That’s a smell called Message Chains.
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 line:
class BoxOfficeScreen {
double priceAt(Ticket ticket) {
return ticket.getEvent().getVenue().getSeatMap()
.seatAt(ticket.getRow(), ticket.getNumber())
.price();
}
}To get a price, the code walks through five classes:
The screens did nothing wrong. They just know too much.
Think about asking for directions in a big building. You ask the guard at the gate, and she says, ask reception. Reception says, try the admin office. And the admin office says, it’s on the third floor.
You get there. But now you depend on every hop. Move the records, or rename an office, and your whole route is wrong.
In code, each hop is a getter, and the route is a chain. The course reference describes a message chain as data reached through a series of calls, where each call depends on the one before it.
Look at the screen again. To be fair, it’s tidy. Every call is a real getter, and it reads like a sentence. Each class does its job, each getter returns something sensible, and nobody wrote anything silly.
The problem is what the screen has to know. It knows a ticket has an event, an event has a venue, and a venue has a seat map: five classes, and the screen is welded to the shape of every one of them. Nothing is wrong in any single line. The dependency is in the route.
Watch what that costs. The venue team splits the seat map into sections. seatAt now takes a
section, as well as a row and a number:
class SeatMap {
private final Map<String, Section> sections;
Seat seatAt(String section, int row, int number) {
return sections.get(section).seat(row, number);
}
}The screen from the hook breaks. So do the mobile app, the refund desk, the upgrade dialog, the ticket printer and the sales report.
Six screens, with the same route copied into each. Not one of them cares about sections. They only wanted a price. That’s the first cost: a change inside the venue ripples outward, to everyone who walked through it. It’s the same shape as Shotgun Surgery, reached through someone else’s structure.
The second cost is testing. To check one price, you have to build an event, a venue, a seat map and a seat.
Software people call the idea behind all this the Law of Demeter, or the principle of least knowledge: an object should talk to its close neighbours, not to strangers. It was proposed at Northeastern University around the end of 1987.
a().b().c().d().getVenue().getSeatMap() and count the results.Event event = ticket.getEvent();
Venue venue = event.getVenue();
SeatMap seatMap = venue.getSeatMap();It’s the same chain, split across lines. The screen still knows every hop.
So here is the insight. The screen isn’t asking for a price. It’s navigating: it knows the route through other objects’ structure. The fix is to stop navigating, and ask. Ask your nearest neighbour, the ticket, for the price. Let it find out how.
A class should know its neighbours, and nothing past them.
The refactoring is called Hide Delegate. The ticket gets a new method, seatPrice, that does
the walking for its callers. But the ticket doesn’t walk the whole route. It asks only its event,
and the event asks only its venue. The venue is the one place that knows about the seat map.
class Ticket {
double seatPrice() {
return event.seatPrice(section, row, number);
}
}
class Event {
private final Venue venue;
double seatPrice(String section, int row, int number) {
return venue.seatPrice(section, row, number);
}
}
class Venue {
private final SeatMap seatMap;
double seatPrice(String section, int row, int number) {
return seatMap.seatAt(section, row, number).price();
}
}Now the screen makes one call: it asks the ticket for its seat price.
double price = ticket.seatPrice();At the scale of a whole subsystem, this is the idea behind the Facade pattern: one simple front, so clients don’t have to learn the structure behind it.
Last, the hops lose their getters. Remove the getter for the venue, or make it private, so nobody can walk the old route again. Now the venue team splits the seat map again, and exactly one method changes. The six screens never notice.
Sometimes a chain means the work lives in the wrong class. Here, a screen decides whether a ticket was discounted:
boolean isDiscounted(Ticket ticket) {
double list = ticket.getEvent().getVenue().getSeatMap()
.seatAt(ticket.getSection(), ticket.getRow(),
ticket.getNumber()).price();
return ticket.getPaid() < list;
}It walks to the seat price, and compares it with what the buyer paid. But what the buyer paid belongs to the ticket. So extract the decision into a method, and move it onto the ticket. That’s Extract Method, then Move Method:
class Ticket {
boolean isDiscounted() {
return paid < seatPrice();
}
}The data and the decision now sit together. The screen asks a yes-or-no question, and gets a
yes-or-no answer: ticket.isDiscounted().
Here is one with a twist. At the refund desk, the code doesn’t read anything. It changes something:
ticket.getOrder().getBuyer().getWallet().credit(ticket.getPaid());It goes from the ticket to its order, then the order’s buyer, then the buyer’s wallet, and then it pays the money in. So the refund desk knows that customers have wallets. It shouldn’t even know wallets exist.
Same fix. The ticket gets a refund method. So does the order. And only the customer touches the
wallet:
// refund desk
ticket.refund();
class Ticket {
void refund() { order.refund(paid); }
}
class Order {
void refund(double amount) { buyer.refund(amount); }
}
class Customer {
void refund(double amount) { wallet.credit(amount); }
}The desk says what it wants, a refund. Each neighbour decides how.
Is every chain of dots a message chain? No. Look at this stream pipeline:
List<String> titles = events.stream()
.filter(e -> e.isOnSale())
.map(Event::getTitle)
.toList();Each call hands back a stream. The code never walks through anyone’s structure. It keeps working on
the same kind of thing. The Java API describes filter and map as intermediate operations that
return a stream. Builders work the same way: each call returns the builder itself, so the calls
chain, by design.
So count the types, not the dots. It’s a smell only when each dot steps into a different object’s insides.
| Chain | Types along the way | Message chain? |
|---|---|---|
events.stream().filter(…).map(…).toList() | a stream, a stream, a stream | no |
builder.event(e).section(s).build() | the builder, the builder, the builder | no |
ticket.getEvent().getVenue().getSeatMap() | Ticket, Event, Venue, SeatMap | yes |
Feature Envy is a close cousin. Envy is a method using another class’s data more than its own. A chain is often the route it takes to reach that data, and the fix for both is to move behavior to where the data lives.
When is it fine to leave a chain alone? When the structure is stable, and public by design.
DayOfWeek day = event.getStart().toLocalDate().getDayOfWeek();Only the first hop is ours. Java’s date types are documented, public, and not about to change. The same goes for plain data objects that exist to be read, like a parsed report.
And there is a case where the fix backfires. Hide every hop, and a class can end up as nothing but forwards:
That’s the next smell, Middle Man, the other side of message chains. The course reference makes the same point: skip the fix if resolving the chain would cause a Middle Man.
A message chain is a client walking through other objects to reach what it needs. Hide the route with Hide Delegate, or extract the work and move it next to its data. Leave fluent calls and stable structures alone, and stop hiding when a class only forwards.
Checklist:
Ask your neighbour, not your neighbour’s neighbour.