Overview
The Law of Demeter (principle of least knowledge) says a method should only talk to its immediate friends: its own fields, its parameters, objects it creates, and itself, not to objects returned by those friends. Code like order.getCustomer().getAddress().getCity() reaches through a chain of objects and couples the caller to the structure of all of them.
Following Demeter means asking objects to do things (order.shippingCity()) rather than navigating their internals. It reduces coupling and makes refactoring easier. It is a guideline, not an absolute: fluent builders and data transfer objects are acceptable chains.
The cashier asks you for payment; you hand over money. The cashier does not reach into your pocket, open your wallet, and pull out the cash. You decide how to pay.
When to use it
- Code has long chains of getters (train wrecks).
- Changing one class's internal structure breaks distant callers.
- Mocks in tests return mocks that return mocks.
Where it shows up in interviews
Recognize it when: a.getB().getC().doSomething().
- Refactor order shipping logic
- Design a car with engine and components
Where it is used in real software
Martin Fowler and the Pragmatic Programmers promote telling objects what to do, which follows Demeter naturally.
Deep mock chains in Mockito tests are a common symptom of Demeter violations.
Builders and streams chain calls on the same or new objects, which is an accepted exception.
Key terms
- Immediate friends
- this, parameters, fields, and locally created objects.
- Train wreck
- Chained getters reaching deep into object graphs.
- Tell, don't ask
- Ask objects to perform behavior instead of extracting data.
- Delegation method
- A method that forwards a request to a collaborator.
How it works, step by step
- 1Spot the chain
order.getCustomer().getWallet().debit(x).
- 2Ask what the caller really wants
To charge the customer.
- 3Add behavior to the nearest object
order.chargeCustomer(x) or customer.pay(x).
- 4Let each object delegate one level
Customer delegates to its wallet.
- 5Avoid adding dozens of trivial wrappers
Rethink responsibilities if delegation explodes.
Reaching vs telling
A delivery service needs the city for an order
| Code | Caller depends on | If Address moves inside Profile |
|---|---|---|
| order.getCustomer().getAddress().getCity() | Order, Customer, Address | Caller breaks |
| order.shippingCity() | Order only | Only Order changes |
NOWCode: order.getCustomer().getAddress().getCity() | Caller depends on: Order, Customer, Address | If Address moves inside Profile: Caller breaks
Each object knows about its neighbors, not the whole graph.
Implementation
class Wallet { constructor(private balanceCents: number) {} debit(cents: number) { if (cents > this.balanceCents) throw new Error("Insufficient funds"); this.balanceCents -= cents; }} class Customer { constructor(private wallet: Wallet) {} pay(cents: number) { this.wallet.debit(cents); } // delegates one level} class Order { constructor(private customer: Customer, private totalCents: number) {} checkout() { this.customer.pay(this.totalCents); }} // Violation would be: order.getCustomer().getWallet().debit(total)new Order(new Customer(new Wallet(10_000)), 2_500).checkout();Complexity and performance
vs whole chain.
Keep them meaningful.
Trade-offs
Strictly following Demeter can add many forwarding methods; if so, the responsibility may belong elsewhere.
Navigating plain data (DTOs, JSON, records) is fine; the law targets objects with behavior.
Variants and related techniques
Chaining on the same builder object is not a violation.
Hides a subsystem's graph behind one object.
Common mistakes
- Treating it as 'count the dots'.
Fix: The concern is coupling to structure, not syntax.
- Returning internals so callers can act.
Fix: Move the action into the owner.
Interview questions
What is a train-wreck call and why is it bad?
A chain like a.getB().getC().doX(). The caller becomes coupled to the internal structure of B and C, so refactoring any of them breaks it; it also usually means behavior lives in the wrong place.
Are builders a Law of Demeter violation?
No. Each builder call returns the same builder or a new value, so the caller only talks to one object it created.
Practice problems
| Problem | Difficulty | What it trains |
|---|---|---|
| Refactor three train-wreck calls | Easy | Tell, don't ask. |
| Model a car so drivers never touch engine parts | Medium | Delegation. |