Let's break Demeter's Law.
Problems
Unnecessary Indirection
Empty Classes
Readability
Solutions
- Remove Middle man.
Sample Code
Wrong
Right
Detection
Same as its opposite smell, We can detect this small using parsing trees.
Tags
Coupling
Declarative
Readability
Conclusion
This is exactly the opposite to Message Chain. We make explicit the message chain.
Relations
Code Smell 08 - Long Chains Of Collaborations
Maxi Contieri ・ Oct 27 '20
More info
%[https://refactoring.guru/smells/middle-man]
%[https://refactoring.com/catalog/removeMiddleMan.html]
%[https://wiki.c2.com/?MiddleMan]
%[https://www.jetbrains.com/help/idea/remove-middleman.html#remove_middleman_example]
%[https://en.wikipedia.org/wiki/Law_of_Demeter]
Credits
Photo by Dan Counsell on Unsplash
Whenever I have to think to understand what the code is doing, I ask myself if I can refactor the code to make that understanding more immediately apparent.
Martin Fowler
Software Engineering Great Quotes
Maxi Contieri ・ Dec 28 '20
This article is part of the CodeSmell Series.