5 ms·
The law of Demeter is sometimes a pain to work with but it makes your codebases easier to refactor and test when there’s no calls to foo.bar.qux.makeWidget() in
by inlined 7y ago
The law of Demeter is sometimes a pain to work with but it makes your codebases easier to refactor and test when there’s no calls to foo.bar.qux.makeWidget() inside a WidgetFactory class. WidgetFactory instead is constructed with multiple single-purpose objects that are easily mocked or refactored.
This however adds complication to constructors in OOP and is what I assume has always been the reason for the rise of Dependency Injection.
- gowld 7y agoPlease don't mock objects if possible. Prefer real, then fake, then mock for testing error conditions that "cannot happen" during normal operation that you can't force of fake in tests. https://medium.com/@CDehning/when-to-use-fakes-instead-of-mocks-c80188b9a3f1 https://medium.com/@CDehning/when-to-use-fakes-instead-of-mo...
- iteriteratedone 7y agoDI is a higher order idea of encapsulation Demeter is not about your current inplementation but about always being ready for refactoring. With the demeter law respected you never have to hunt down dependencies. They are obvious and restricted to signatures. Not internal function logic.
- johnsimer 7y agoDependency injection plus LoD has made designing new modules for me a breeze, and has drastically reduced my number of bugs