What the Law of Demeter is
The Law of Demeter is a software design guideline that encourages a unit of code, such as an object, to interact only with its immediate collaborators rather than reaching through them to access more distant objects. Sometimes summarized as the idea of only talking to your close friends, it aims to limit how much one part of a program needs to know about the inner structure of others.
Talking to close collaborators
The guideline suggests that an object should call methods on itself, on objects it directly holds, or on things directly provided to it, but should avoid reaching deep into the internals of other objects to call methods on their parts. In other words, it should deal with its immediate collaborators and not navigate through them to distant objects. This keeps interactions local.
Reducing coupling
By limiting interactions to close collaborators, the Law of Demeter reduces coupling between parts of a program. When an object reaches through others to distant objects, it becomes dependent on the internal structure of those others, which makes the code fragile. Following the guideline keeps each object less entangled with the internals of others, so changes are less likely to ripple widely.
Why it helps
Adhering to the Law of Demeter helps make software more maintainable and robust. Because objects do not depend on the deep internal structure of others, changes to those internals are less likely to break distant code. The resulting looser coupling makes the system easier to understand and modify. This reduction in hidden dependencies is the main benefit of the guideline.
Applying the guideline
The Law of Demeter is a guideline to be applied thoughtfully rather than a rigid rule. Its spirit is to avoid unnecessary reaching through objects, which creates brittle dependencies. Understanding the Law of Demeter clarifies a useful principle for keeping interactions between parts of a program local, which supports looser coupling and more maintainable code.
Why it matters
The Law of Demeter is an important guideline in software design. Understanding what it is - interacting only with close collaborators rather than reaching through them - clarifies how to reduce coupling and keep code maintainable. For anyone learning software design, it is a valuable concept to understand.
Related on Skillo
See also: What is coupling in software? Explained, What is encapsulation? Explained.
Sources
Published date reflects the original event date (2026-10-19). This article is original Skillo editorial written from the sources above; facts were verified in September 2026.
Leave a Reply