Law of Demeter: Why Objects Should Talk Only to Their Closest Neighbors
The Law of Demeter, also called the Principle of Least Knowledge, is a software design guideline that restricts how deeply one object can reach into another's internal structure. Originating from a research project named after the Greek goddess Demeter, it was developed to reduce maintenance costs in object-oriented systems by limiting class coupling. The principle states that a method should only call methods on the object itself, its parameters, objects it creates, or its direct components — avoiding so-called 'train wreck' chains like object.getA().getB().doSomething(). This kind of deep chaining creates implicit coupling, fragility during refactoring, and makes unit testing harder, all of which have been empirically linked to higher defect rates in studies such as the 1994 Chidamber and Kemerer CK metrics. The practical fix is to delegate behavior inward — for example, calling client.pay(50) instead of directly accessing client.wallet.remove(50) — so external code depends only on a clean public interface.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in