Demeter’s Law: Don’t talk to strangers!
Tags: typescript, javascript, demeterlaw, cleancode
Published: 2019-03-08 07:50:19

---
title: Demeter’s Law: Don’t talk to strangers!
published: true
description: 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.
tags: typescript, javascript, demeter-law, cleancode
cover_image: https://thepracticaldev.s3.amazonaws.com/i/oaaezqpeydtpe6050kxb.jpg

---


> 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.
1.  Each unit should only talk to its friends; *don’t talk to strangers*.
1.  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:

![](https://cdn-images-1.medium.com/max/1000/1*INzo8h9xDf-l6h1HdVMTuA.png)

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.

![](https://cdn-images-1.medium.com/max/1000/0*SQJijRVd2Xeom69p.png)

### 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`.

![](https://cdn-images-1.medium.com/max/1000/0*hV9tWEUwQulX2eyH.jpg)

The original code is from
[https://github.com/tavaresasilva/LoDRaV](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.

![](https://cdn-images-1.medium.com/max/1000/1*3iBw_gPCWpsZl9Q0O-aoSw.png)

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.

![](https://cdn-images-1.medium.com/max/1000/1*PFUBDkjzfC7pF_EYoYtrXg.png)

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

![](https://cdn-images-1.medium.com/max/1000/1*F-gCvOlI_z51vV9-QIMleg.png)

![](https://cdn-images-1.medium.com/max/1000/1*PoY1fyqgeCibRbZwCG3Q1w.png)

![](https://cdn-images-1.medium.com/max/1000/1*DAu5brBVNYzsSK0eTKwCKA.png)

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:

![](https://cdn-images-1.medium.com/max/1000/1*rhr6ncJTK1vuiSyouWg8nQ.png)

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

![](https://cdn-images-1.medium.com/max/1000/1*sNQRAcDWinypyzoCtz5FuQ.png)
<span class="figcaption_hack">Type caption for image (optional)</span>

### Advantages

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

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

### More, more and more

[http://www.ccs.neu.edu/home/lieber/LoD.html](http://www.ccs.neu.edu/home/lieber/LoD.html)
[https://en.wikipedia.org/wiki/Law_of_Demeter](https://en.wikipedia.org/wiki/Law_of_Demeter)
[https://hackernoon.com/the-law-of-demeter-in-the-era-of-microservices-3186f4c399a1](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](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.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/LoD-2011-Zurich.pdf)
[http://www.ccs.neu.edu/home/lieber/LoD/law_of_demeter_healthy_code-external.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](https://dzone.com/articles/the-beautiful-law-of-demeter)

— 

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

*****

Originally published at [www.carloscaballero.io](https://carloscaballero.io/demeter-law/)