We all know that clean code principles are very helpful in developing software, which is going to generate fewer bugs and be easier in maintaining. Today I would like to focus on one them.
What is the Law of Demeter?
In shortcut “speak only with friends”. It’s essential idea is that methods of certain class should only call operations:
– of same object
– of any of its parameters
– of any object created by it
– of any ingredient of class, that this method belongs
Examples
<Some code in space and time>
Summary
The advantage of respecting the Law of Demeter is decreasing the count of interactions and hence enhance flexibility and the possibilities of expanding our system. It improves debugging and testing the solution. Unfortunately, it has its flaws namely the increased amount of methods. They are going to be mainly used to invoke more and more methods from different classes. It’s playing delegates role.
As a curiosity, I can say that this principle was formulated in 1987 by Ian Holland. But probably most of us found out about it after reading uncle Bob’s “Clean code” 😉
