Limiting Coupling - the Law of Demeter

At one time there was a great deal of interest in metrics, easy to measure characteristics of programs that if controlled would tend to achieve some desired goal. Thus, for example, people said that no function should ever be more than 40 lines long, or so object have more than 20 methods.

Here is an interesting metric that grew out of work on the Demeter project at Northwestern University, and was hence called the law of demeter. It is an attempt to limit the way in which one component can interact with another component.

Law of Demter. In any Method M attached to a class C, only methods defined by the following classes may be used:
1.
The instance variable classes of C.

2.
The argument classes of method M (including C); note that global objects or objects created inside the method M are considered arguments to M.
[audio] [real] Text to accompany slide8, in Chapter 17 of An Introduction to Object-Oriented Programming