Law of Demeter (LoD)

The Law of Demeter was originally formulated as a style rule for designing object-oriented systems. "Only talk to your immediate friends" is the motto. The style rule was discovered at Northeastern University in the fall of 1987 by Ian Holland.

A more general formulation of the Law of Demeter is: Each unit should have only limited knowledge about other units: only units "closely" related to the current unit. Or: Each unit should only talk to its friends; Don't talk to strangers.

In this general form, the LoD is a more specific case of the Low Coupling Principle well-known in software engineering. The Low Coupling Principle is very general and we tried to make it more specific. The benefit of the specific Law of Demeter shown below is that it makes the notion of unnecessary coupling very explicit.

The main motivation for the Law of Demeter is to control information overload; we can only keep a limited set of items in short-term memory and it is easier to keep them in memory if they are closely related. The definition of "closely related" is intentionally left vague so that it can be adapted to particular circumstances.

In the application of LoD to object-oriented design and programming we have:
unit = method f
closely related = methods of class of this/self of f and other argument classes of f and methods of immediate part classes (both computed and stored) of class of f (classes that are return types of methods of class of this/self (= computed) and the classes of data members (= stored)) and methods of classes of objects that are created in f.

The above is the class form of the Law of Demeter which has the advantage of being efficiently computable. However, it is the Object Form of the Law of Demeter which expresses the style rule we really want. Unfortunately, whether a program satisfies the object form is undecidable. However, we can run the program and check at run-time whether violations occur (this is a nice exercise in Aspect-Oriented Programming).

Rumbaugh summarizes the Law of Demeter as: A method should have limited knowledge of an object model. This view leads to aspect-oriented programming: we pull out the method and transitively all its auxiliary methods into a separate aspect. This works best if the separate aspect has limited knowledge about the object model.

The Law of Demeter and narrow and wide interfaces (initiated by Mitch Wand 2003, invited talk at ICFP 2003):
At the method level, the LoD leads to narrow interfaces because each method needs to know about a small set of methods of closely related objects. On the other hand, at the class level, the LoD leads to wide interfaces because the LoD requires that we introduce many auxiliary methods instead of digging directly into the object structures. Wide interfaces for classes are a problem and the solution is an aspect-oriented approach where the behavior of the method is specified as an aspect at a high level of abstraction. We still have wide interfaces but we manage them through a language that specifies implementations.

Viewgraphs on LoD

Back to Demeter home page

Professor Karl Lieberherr
College of Computer and Information Science Science, Northeastern University
Boston, MA 02115
lieber at