Law/Demeter
Law of Demeter
- Statement: a method may call methods only on itself, its parameters, objects it creates, and its own direct component objects — never reach through one object to act on something that object merely holds. Law of Demeter (General Formulation): "Only talk to your immediate friends" / "Each unit should only talk to its friends; Don't talk to strangers."
- Origin: discovered at Northeastern University in the fall of 19/8/7 by Person/Ian Holland, while working on the Demeter Project; first published in Lieberherr & Holland, "Object-Oriented Programming: An Objective Sense of Style," OOPSLA '88. Law of Demeter: Principle of Least Knowledge (Karl Lieberherr's own page): "The name 'Law of Demeter' was chosen because the style rule was discovered while working on The Demeter Project ... (Demeter = Greek Goddess of Agriculture; grow software in small steps.)"
- Kind: heuristic — a specific case of the general Low Coupling Principle, adopted as a style rule rather than proved as a theorem. Law of Demeter (General Formulation): "In this general form, the LoD is a more specific case of the Low Coupling Principle well-known in software engineering."
- Example: flagged in My/Pref/Dev/Tool/Secrets/fnox against the 1Password CLI, whose finest-grained access boundary is the Vault — any script that needs one secret ends up holding standing access to every secret in that Vault, the law's warning made concrete.
See also