The Law of Demeter
"Each unit should talk only to its immediate friends, and never to strangers."
"Each unit should talk only to its immediate friends, and never to strangers."
A method should call methods on things it already knows about (itself, what it was handed, what it holds, what it just made) and not go reaching through those things to get at their internals. Reaching further means knowing the shape of somebody else's structure, and that knowledge becomes a dependency on it.
Rated Heuristic: a dependable rule of thumb, not a proven theorem. The other 224.
Formally, a method may invoke methods only on: itself, its own parameters, objects it creates, its own attributes, and accessible globals. The practical shorthand is 'use only one dot': a chain like a.getB().getC().doThing() states, in code, that the caller knows the internal structure of A and of B. It is now coupled to two designs it does not own, and either one changing shape breaks it: a break at a distance, in a file whose author never heard of it. Keeping calls one hop away limits how far a structural change can travel.
It converts an abstract worry about coupling into a rule you can apply while reading a single line, which is rare and valuable. It is also the clearest statement of why encapsulation is not about making fields private: a getter that returns an object you then reach into has exposed the structure just as thoroughly as a public field would.
The standard criticism is the cost of obeying it: propagating calls through wrappers takes time and space at runtime, and done poorly it inflates a class's interface with auxiliary methods that exist only to forward. That is a genuine trade: maintainability bought with complexity and some efficiency. It also does not apply cleanly to data-transfer objects, to fluent interfaces, or to standard-library types whose structure is stable by contract, and mechanical enforcement by counting dots produces false positives in all three.
It is not a law in any sense stronger than a design guideline, and the name is a project name, not a claim to lawfulness. It is not a rule against method chaining as such. And 'don't talk to strangers' does not forbid using objects you were given. Those are friends; strangers are the objects you reach through them to find.
Real as a rule of thumb, not as a theorem. The Law of Demeter is rated Heuristic here: dependable enough to plan with, and with no proof behind it.
Rated from two sources. 225 of the 1,116 entries carry this rating. How the scale works.
The best-known laws are the least likely to rest on measurement; the full verdict on The Law of Demeter.
Proposed by Ian Holland at Northeastern University in late 1987, during work on the Demeter Project: an adaptive-programming research effort named after the Greek goddess of agriculture, chosen to signal a bottom-up philosophy of programming and as a sister name to Zeus, the project's hardware description language. So the law is named after a goddess by way of a research project, not after a person who stated it.
Everything above traces to en.wikipedia.org and ieeexplore.ieee.org, two sources in all, one of them the original publication rather than an account of it. Nothing is written from memory, and the method is written down. Spot an error or a better source? Suggest a fix.