CSE Roadmap

Core tier · about 3 hours · 6 parts

Part 5 of 6 · 3 min read

Demeter

Talk to friends, not strangers.

Reducing coupling with the Law of Demeter

Coupling often hides in long call chains. Suppose a booking wants the city of its customer.

String city = booking.customer().address().city();

This one line knows that a booking has a customer, that a customer has an address, and that an address has a city. If the customer class later stores a list of addresses, every such chain in the program breaks.

HF names the cure the principle of least knowledge: an object should deal only with its close friends. CC calls it the Law of Demeter, and states it exactly. A method of class C should only call methods on these things: C itself, objects the method creates, objects passed in as arguments, and objects held in fields of C. It should not call methods on objects that those calls return. In short, talk to friends, not strangers. CC calls long chains train wrecks, because they look like linked carriages.

HF's fix is to add a method that makes the request on the caller's behalf. Here:

class Customer {
    private final Address address;
    Customer(Address a) { address = a; }
    String city() { return address.city(); }
}

class Booking {
    private final Customer customer;
    Booking(Customer c) { customer = c; }
    String customerCity() { return customer.city(); }
}

Now booking.customerCity() returns "Pune" for a customer who lives in Pune, and the caller knows only the booking. If addresses change, only Customer is edited.

CC adds a fair note. The law applies to objects, which hide their insides and expose behaviour. It does not apply to plain data structures, which are meant to expose their fields. A record that only holds values can be read freely. The trouble starts when a getter-heavy class pretends to be an object but gives away its inside.

There is a cost too. HF notes that applying the principle gives more small forwarding methods, which adds some complexity and run time. Do not forward every call. Use it where the chain reaches deep into another object's structure.

Common mistake

Breaking a chain into three lines with three variables and thinking the problem is solved. The code still knows the same structure. Add a method to the nearest object instead.

Interview angle

Answer in your head first, then open it.

What are coupling and cohesion?

Cohesion is how closely a class's parts belong together. Coupling is how much a class depends on others. Aim for high cohesion and low coupling.

How do you check a class for cohesion?

See which methods use which fields. Groups that never meet are separate classes.

What is the Law of Demeter?

A method should call only its own class, its arguments, objects it creates and its own fields, not what those calls return.

Check yourself: 1. What does booking.customerCity() return for a Pune customer? 2. List the four things a method may call under the law. 3. Why does the law not apply to plain data holders?

Words you will meet

Word Meaning
Cohesion How closely a class's parts belong together
Coupling How much one class depends on another
Law of Demeter Talk to friends, not to the objects they return
Train wreck A long chain of calls through several objects
Data structure A holder of values that exposes them openly