Saturday, September 17, 2005

Law of Demeter (LoD), Low Coupling

One of the bigger challenges in software engineering is keeping the complexity of the design under control. Two major techniques to reduce complexity are reducing coupling and increasing the cohesion of the software components/objects/methods.

The Law of Demeter is a style rule for designing object-oriented systems. "Only talk to your immediate friends". The rule was discovered at Northeastern University in 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.

The LoD is a more specific case of the Low Coupling Principle well-known in software engineering. The Low Coupling Principle is very general. The benefit of the Law of Demeter 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.

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.

Read more on the Northeastern University page on the Law of Demeter


Post a Comment

Read more about Software Quality at the <<Software Quality Weblog Home