Sitemap
Better Programming

Advice for programmers.

Demeter’s Law: Don’t talk to strangers!

Software Engineering Principles

4 min readSep 26, 2019

--

Press enter or click to view image in full size

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:

  1. Each unit should have only limited knowledge about other units — only units “closely” related to the current unit.
  2. Each unit should only talk to its friends — don’t talk to strangers.
  3. 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.

--

--

Carlos Caballero
Carlos Caballero

Written by Carlos Caballero

Hi! My name is Carlos Caballero and I’m PhD. in Computer Science from Málaga, Spain. Teaching developers and degree/master computer science how to be experts!

Responses (3)