DEV Community

Cover image for Demeter’s Law: Don’t talk to strangers!
Carlos Caballero
Carlos Caballero

Posted on

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

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 when he and his 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.

The Demeter’s law is known as don’t talk to strangers because any method of
an object only can call to methods of:

  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.
  • A global variable, accessible by O, in the scope of m.

In summary, all of the above rules can be stated as that you should avoid
invoking methods of a member object returned by another method. In the modern
object oriented languages the identifier used is dot or ->. Therefore the
Demeter's law is violated when the code has more than one step between classes,
i.e the following code show an example in which Demeter's law is violated:

In this case, an object a from the A class can request a method of an object
instanced of B class but the object A should not reach object B directly due
to that would mean the object A has greater knowledge of object B's internal
structure (tight coupling).

The following image illustrated who are friends between classes relations.

Real example — Person → House → Address

Now, I am going to show a real example implemented using TypeScript as
programming language. In the following UML diagram you can see as a Person is
related with House and House is related with Address.

The original code is from
https://github.com/tavaresasilva/LoDRaV
and it’s coding using JAVA.

The following code can be run in the client/context whereas the first code broke
Demeter’s Law due to Person requires a knowledge about the inner
implementation of the class House. On the other hand, the second
implementation respects Demeter's Law and the code is less coupled.

The following steps shown as you must implemented the code to respect Demeter’s
Law and obtain a code less coupled. So, the first step is created the interfaces
which will be implemented in our concrete classes.

The next step will be the implementation of the concrete classes as you can seen
below.

The most important in the code is that no method violated Demeter’s Law (there
is not more than two consecutive invocations of contained objects).

Another example in which the Demeter’s Law is broken is the following one:

In this case, the solution is implemented isZipCode method in class person
which you can see in the following code:


Type caption for image (optional)

Advantages

The main advantages of satisfying the Demeter’s Law are the following:

  1. Dependencies between classes and coupling are reduced.
  2. Reuse the classes with ease.
  3. The code is easier to test.
  4. The code is more maintainable and flexible to changes.

More, more and more

http://www.ccs.neu.edu/home/lieber/LoD.html
https://en.wikipedia.org/wiki/Law_of_Demeter
https://hackernoon.com/the-law-of-demeter-in-the-era-of-microservices-3186f4c399a1
https://testing.googleblog.com/2008/07/breaking-law-of-demeter-is-like-looking.html
http://www.virtuouscode.com/2011/07/05/demeter-its-not-just-a-good-idea-its-the-law/
http://www.ccs.neu.edu/home/lieber/LoD/LoD-2011-Zurich.pdf
http://www.ccs.neu.edu/home/lieber/LoD/law_of_demeter_healthy_code-external.pdf
https://dzone.com/articles/the-beautiful-law-of-demeter

The GitHub branch of this post is https://github.com/Caballerog/blog/tree/master/demeter


Originally published at www.carloscaballero.io

Discussion (10)

Collapse
mariotk profile image
MarioTK • Edited

Dear Dr.Caballero,

I'm sorry to disturb you. I'm a computer science student

Demeter's law should simplify the dependency graph, reducing the overall coupling. The objective of this law is to improve encapsulation.

An object of class B (status information of A) should not return to A an object of class C (status information of B).

Right?

Thank you

Collapse
davidsusu profile image
Dávid Horváth

But "person.isZipCode(zipCode)" is (reverse) coupled to the consumer, isn't it? Even worse, we must also change House for getting zipcode or any other "random" information, such as "person.isLivingInCircle(geoCoords, maxDist)" or "person.isZipCodePrime()".

Collapse
matteobiagetti profile image
Matteo Biagetti

Great post!

The GitHub repo seem not reachable...

Collapse
carlillo profile image
Carlos Caballero Author

Hi Matteo!

I just fixed!

Thanks.

Collapse
abdurrahmaanj profile image
Abdur-Rahmaan Janhangeer

saved!

Collapse
carlillo profile image
Carlos Caballero Author

Thanks! Any question or comments are welcome! 😊

Collapse
manuelmolina97 profile image
Manuel

Great post! Keep it up!!

Collapse
carlillo profile image
Carlos Caballero Author

Thanks Manuel!

Collapse
olivermensahdev profile image
Oliver Mensah

Awesome Post.

Collapse
carlillo profile image
Carlos Caballero Author

Thanks Oliver 😊!