Recently I have read some articles that have mentioned the Law of Demeter, and not having heard of it, I went looking for it. Blatantly stolen from the article, the Law of Demeter is as follows:
A method “M” of an object “O” should invoke only the
the methods of the following kinds of objects:
- itself
- its parameters
- any objects it creates/instantiates
- its direct component objects
Why is this a good thing? In simple terms, following the Law of Demeter, you enforce encapsulation of a method’s implementation and dependencies on other objects. Should that method have to change at a later time, it minimizes the impact on callers of the method, localizing the change to the method in question and its subordinates.
For more information, and perhaps a more succinct explanation, see the linked article above.