Skip to main content

Command Palette

Search for a command to run...

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

Published
0 min read
Demeter's Law: Don't talk to strangers!
C

I'm PhD. in Computer Science from Málaga, Spain. Currently, I am teaching developers and degree/master computer science how to be experts in web technologies and computer science.

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:

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

A

Hi fellas! I want a bit of advice myself for future lawyers. If you wish to reach more prospective clients and get more phone calls to your law office. You need the right SEO agency specializing in SEO for lawyers and law firms. SEO agency helps you rank higher in Google SERPs than your competitors and get leads from local search, blog articles, directories, etc. So, if you be a rich lawyer, I think you already know what you need to do;)

J

Hello everyone, Great post. The time has come that every person should have a lawyer who will help and advise his client in any situation. And if you haven’t found a good lawyer yet, then you need to contact Paul Mankin .

M
Mark6y ago

I've tried to be really strict about this for a little while, but I found that in some cases it's just too hard and not really worth the effort.

Have others had the same experience?

I still think it's a good guideline, but I consider it more a "Guideline of Demeter" than "Law of Demeter".

Famous example, is accessing the pages of a book through the book reference really a problem? It's not really an "implementation detail", it's just how books work, shouldn't it be considered part of the API?

2
C

Hi!

In computer science the laws are dangerous. In my opinion, each law/technique should be taken as an advise.

Thanks

1
J

At least, laws and "best practices" are a reasonable starting point \ safe assumption. But there are certainly cases where "best practices" are abused. Don't repeat yourself is one of the worst offenders in my book. Please don't wire EVERYTHING together. O_o Obviously, config \ and settings + constants shouldn't be repeated everywhere. But the number of classes I have seen cobbled together into a super class, in the spirit of DRY. old man grunt

1
M
Mark6y ago

Carlos Caballero Joseph S Stevens Yeah they're not inviolable Laws of Nature. But some apply more generally than others.

The subset of the Guideline of Demeter that I do adhere more strictly to is to not mutate fields of members of members (or really, it's on the object to not expose things that can mutate their internals).

2

More from this blog

Carlos Caballero

41 posts