jargon

Comparison

Law of DemetervsMessage chain

Law of Demeter

you wrote order.getCustomer().getAddress().getPostcode() and a change three objects away broke your line.

Talk only to your immediate collaborators, not to their collaborators' collaborators. Each dot in a chain like that is a structural assumption about somebody else's internals, so a chain of four is four ways for a change elsewhere to break you. The point is not the dot count — it is that you have coupled to a shape rather than to a service, and the fix is usually to ask the first object for the answer rather than for its parts.

Full entry →

Message chain

the line is four dots long and a change to any object along the way breaks the code at the end of it.

Navigating through a chain of objects to reach the one you actually want, which couples the caller to every structure along the path. It is the violation the Law of Demeter names, and the standard fix is to ask the nearest object for what you need so the traversal happens where the structure is known. Fluent interfaces look identical and are not the same thing — each call there returns the same receiver by design.

Full entry →

Related comparisons