Hello everyone, today I will be talking about the Principle of Least Knowledge (AKA Law of Demeter). When first looking into this topic I was unsure of exactly what this was and how it would be applied to programming. When doing my research I found the Khouri College of Computer Sciences at North Eastern University had a page dedicated to this topic, where this law was first introduced.
General Formulation

The LoD is essentially a simple style rule for designing object oriented systems.
“Only talk to your immediate friends” is the motto.
Professor Leiberherr, the author, states a formulation of “Each unit should have only limited knowledge about other units: only units “closely” related to the current unit.” Its main motivation is to control information overload thus helping memory management as each item is closely related.
You can informally summarize the Law with these three formulations:
- Each method can only send messages to a limited set of objects, namely to the argument objects and to the immediate subparts of the class to which the method is attached.
- Each method is “dependent” on a limited set of objects (organize dependencies)
- Each method “collaborates” with a limited set of objects (organize collaborations)
To formulate the Law we can choose from the following independent possibilities:
- Object/Class
- Class formulation is intended to be compile-time
- Object formulation is intended to be followed conceptually
- Messages/Generic functions/Methods
- Weak/Strong
- If we interpret it as all instance variables, including the inherited ones, we get the weak form of the Law. If we exclude the inherited instance variables, we get the strong form of the Law.
Benefits
In a paper written by Leiberherr, there are a couple facts stated for the benefits:
- If the weak or strong LoD is followed and if class A’s protocol is renamed, then at most the preferred client methods of A and A’s subclasses require modification.
- If the weak or strong LoD is followed and if the protocol of class A changes, then only preferred client methods of A and its subclasses need to be modified and only methods in the set of potential preferred clients of A and its subclasses need to be added
- There’s even more benefits highlighted in the paper pertaining to limiting information overload.
Final Thoughts:
Prior to this webpage I knew nothing about this law/principle, however I now understand that it is a fairly useful rule or its respective use case. The law teaches you a way to program Classes, Inheritance, Abstraction, and a few other techniques. Infact There is so much more depth to this that I cant even fully fit it into this blog post. I would highly recommend you check out this page as It contains all the information you need along with sources to learn this.
From the blog Petraq Mele blog posts by Petraq Mele and used with permission of the author. All other rights reserved by the author.








