Law of Demeter

The Law of Demeter (LoD) is a simple style rule for designing object-oriented systems. "Only talk to your friends" is the motto. The style rule was first proposed at Northeastern University in the fall of 1987 by Ian Holland and popularized in books by Booch, Budd, Coleman, Larman, Page-Jones, Rumbaugh and others. A 2000 book that describes it well is The Pragmatic Programmer by Andrew Hunt and David Thomas. The name "Law of Demeter" was chosen because the style rule was discovered while working on the The Demeter Project which ever since was strongly influenced by the Law of Demeter. The Demeter Project develops tools that make it easier to follow the Law of Demeter. (Demeter = Greek Goddess of Agriculture; grow software in small steps.) For example, DemeterF is an exciting Java tool to follow the Law of Demeter in Java.

In 2003/2004 the Law of Demeter was revisited for Karl Lieberherr's ICSE 2004 keynote paper and presentation. The Law of Demeter was refined from "Only talk to your friends" to "Only talk to your friends who share your concerns" and this refined form is called the Law of Demeter for Concerns (LoDC). The LoDC is best followed by using Aspect-Oriented Software Development (AOSD) techniques such as AspectJ or DJ. On the other hand, the LoDC leads to better AOSD. Properly following the LoDC has two key benefits: It leads to better information hiding (using the technique of structure-shyness or the more general technique of concern-shyness) and to less information overload for the software developer.

This paragraph is added in September 2011. I am pleased that there is continued attention in the software development domain to the Law of Demeter. The Clean Code Talks series in the Google Tech Talks refers to the Law of Demeter as a promoter of clean code. Here is an enjoyable talk by Miskov Hevery from Google. The Clean Code Talks - Don't Look For Things! I also recommend: OO Design for Testability also by Miskov Hevery. There is also continued activity in the blog sphere. This search finds about a dozen posts from Google that mention the Law of Demeter. From the Google "Testing on the Toilet" Loose Coupling with Demeter. Here is another recent example from July 2011: Demeter: It's not just a good idea. It's the Law by Avdi Grimm in Virtuous Code. A recent paper from the University of Zurich from Fall 2011: An Empirical Validation of the Benefits of Adhering to the Law of Demeter (local copy: An Empirical Validation of the Benefits of Adhering to the Law of Demeter ) Conclusion: quote: "we conclude that obeying the Law of Demeter is a straight-forward approach for developers to reduce the number of bugs in their software."

This paragraph is added June 2013: The talk by Ben Orenstein at RailsConf 2013 provides a very good visualization of the Law of Demeter. See his first lightening talk at the beginning: Visualizing Law of Demeter. His topic is interesting: How to talk to Developers. You learn to sing at the end of the talk!

Synonyms: Principle of Least Knowledge.

Back to home page

Professor Karl Lieberherr
College of Computer and Information Science, Northeastern University
Boston, MA 02115
lieber at CCS dot NEU dot EDU
Fax: (617) 373 5121