Vol. I a living index of named laws · est. mmxxvi no ads · no account
The Law Tomeindex of named laws

The Law of Demeter

also known as — The principle of least knowledge, Only talk to your immediate friends, Don't talk to strangers, The one-dot rule
"Each unit should talk only to its immediate friends, and never to strangers."
In plain English

What does The Law of Demeter mean?

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.

Reliability

Rated Heuristic: a dependable rule of thumb, not a proven theorem. The other 224.

Lineage
1987COINED
How it works

How does The Law of Demeter work?

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.

Where you'll see it

What are examples of The Law of Demeter?

The chain that breaks at a distance order.getCustomer().getAddress().getPostcode() ties the caller to the internals of Order, Customer and Address. Changing how an address is stored breaks code three types away.
The paperboy and the wallet The teaching example: a paperboy who reaches into the customer's pocket, takes the wallet and removes the money. Correct in outcome, wrong in structure: the customer should be asked to pay.
Tell, don't ask Instead of pulling a value out to make a decision about it, send the decision inward: order.chargeTo(paymentMethod) rather than reading the balance and comparing it outside.
Why it matters

Why does The Law of Demeter matter?

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.

Working with it

How do you apply The Law of Demeter?

  • Count the dots, then ask why — the count is a smell rather than a verdict. Two dots on a fluent builder or a stream pipeline are fine; two dots that walk somebody else's object graph are the case the rule is about.
  • Move the behaviour to the data — if you keep fetching three things from an object to compute something, the computation probably belongs on that object.
  • Exempt data structures — the rule protects behaviour, not records. A plain data object with no logic has no internals to hide, and applying the rule to it just adds wrappers.
Where it breaks down

What are the limits of The Law of Demeter?

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.

What it doesn't say

What are common misconceptions about The Law of Demeter?

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.

Is it real?

Is The Law of Demeter real?

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.

Occasionally printedin printed books: one of 240 names in its band, of 903 measured
25 of 69entries in software & systems carry the same rating

The best-known laws are the least likely to rest on measurement; the full verdict on The Law of Demeter.

Origin

Where did The Law of Demeter come from?

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.

How the name spread

When did people start saying “The Law of Demeter”?

1800coined 19872019
How often “Law of Demeter” appears in printed English, which takes hold around 1897 and peaks around 2008. The phrase was already in print some 90 years before 1987, the date recorded for the idea here. A chart of words is not a chart of when anyone had the thought. Source: Google Books Ngrams.
Sources

Sources & further reading

  1. 1"Law of Demeter." Wikipedia — Ian Holland's authorship at Northeastern in 1987, the Demeter Project naming, the formal statement of which methods may be called, and the documented criticisms.secondary
  2. 2Lieberherr, K. & Holland, I. (1989). "Assuring Good Style for Object-Oriented Programs." IEEE Software 6(5). — the law as published by the Demeter group.primary

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.

What we checked

  • 1,116entries in the tomeListed one by one on the browse page.
  • 1,116of them citing a sourceOpen any entry: its sources sit at the foot of the page.
  • 2,489source references in allEach is named, and linked wherever the source is online.