Member-only story
Demeter’s Law: Don’t talk to strangers!
Software Engineering Principles
The Law of Demeter (LoD) or principle of least knowledge is a design guideline for developing software, particularly object-oriented programs.
— Wikipedia
This law was proposed by Ian Holland in 1987. Holland and colleagues were programming a system called Demeter using oriented object programming. During the development of the system, they realized that the code that fulfilled a series of rules was less coupled.
Demeter’s law is known as “don’t talk to strangers” because:
- Each unit should have only limited knowledge about other units — only units “closely” related to the current unit.
- Each unit should only talk to its friends — don’t talk to strangers.
- Only talk to your immediate friends.
More formally, the Law of Demeter requires that a method m of an object O may only invoke the methods of the following kinds of objects:
- O itself.
- m’s parameters.
- Any objects created/instantiated within m.
- O’s direct component objects.

