81 points · by cardamomo · 7y ago · 48 comments
As noted elsewhere, this is not really a "law" - it's a design guideline that many find (usually) helpful. However, it was named a "law" and the name has stuck.
It is a law in the regulatory rather than scientific sense, in that it was adopted as a coding rule in the Demeter project.
Good object should be like like stereotypical reserved introvert Finn: respect the privacy of others, don't talk to strangers, keep only few close friends.
Who? Huck Finn? or a cartoon character?
C2 page: http://wiki.c2.com/?LawOfDemeter
This however adds complication to constructors in OOP and is what I assume has always been the reason for the rise of Dependency Injection.
https://medium.com/@CDehning/when-to-use-fakes-instead-of-mo...
Code that demands more communication then what people are comfortable with is bound to fail (become unmaintainable and produce many bugs), so the law of demeter codifies communication structures as a law about code dependencies and architecture.
It follows that for small teams and personal projects the law of Demeter might not be worth following since they don't suffer from these communication problems. Also microservices an application of the law of Demeter to larger chunks of code instead of just classes.
In object oriented programming classes are basically black boxes of code with well defined interfaces. In that sense modules, packages and microservices are just new types of "classes" we invented since the law of Demeter was formulated in '87. The purpose of all of those constructs is just to give a clear interface to a black box of code, just that those black boxes get successively larger.
a.d() <=> a.b().c().d()
Which call should you make?Obviously a.d(), not only because it is shorter but because it makes your code independent of the fact whether a.b() returns something that understands c() which returns d() whose result is what you want.
a.d() is better code then than a.b().c().d() because it makes fewer assumptions about the rest of the system.
Now assume such a.d() does NOT exist. Would it make sense to create it? Yes because THEN you will be able to write better code by more simply writing a.d() .
Now there is a cost to writing a new method a.d(). But typically that cost is not much because its implementation is simple "return this.b().c().d()" .
But then you realize b().c().d() should be replaced by b.d() to follow the Law of Demeter again. So you actually must pay the price of writing two new methods if you want to follow Demeter. Is it worth it? It depends on how much you value maintainablity vs. having to write less code right now.
My guess is that following Demeter is almost always better.
I prefer a more relaxed form of the law: you can do something like a.b().c(), where b is a method that returns an interface. I think of it as a "composite interface". You can repeat the nesting as much as you like, however it gets a little hard to read after a while.
When I'm thinking about an algorithm for solving a problem I sometimes need extra info from many other places. I sometimes need to use globals to get around all this message passing nonsense that would otherwise clutter my function calls with 50 parameters.
I'd use GoTos if they were better supported.
I don't usually program for others, and I don't call myself a good programmer though. I usually just need to solve my own problems, then move on to the next.
If your function call needs 50 parameters (I assume this is hyperbole) then you need some encapsulation. Many codebases with complex functions actually create a struct for the function call, which actually adds forwards compatibility if new struct members are optional.
If you use globals to pass state then you’re probably not thread safe and you’re certainly going to have problems testing because you’ll need to “prep” global state before every test.
There's an object asking to be born!
One example, sometimes DRY'ing up things that just happen to use the same logic now, but don't existentially/necessarily use the same logic. When they stop using the same logic, you add extra 'magic flags', or you've got to undo the architecture.
Sandi Metz says "prefer duplication over the wrong abstraction ". https://www.sandimetz.com/blog/2016/1/20/the-wrong-abstracti... She also writes about how devs are reluctant to change existing abstractions (for both rational and irrational reasons), making the wrong abstraction even more expensive.
Which isn't to say that lots of duplication all over the place is a good sign either, obviously not.
The DRY principle was created by the authors of The Pragmatic Programmer, and as they formulated it, your example is not DRY.
DRY is about requirements, not code or logic. The specification of a requirement should exist in only one place in the code. It aims to reduce instances of "The requirement changed, and I thought I fixed all the places in the code, but I forgot one and it became a bug." If two pieces of code have very similar logic/code structure, but are tied to different features, then the DRY principle absolutely does not suggest you should refactor to make that logic appear in only one place.
Pretty much every time I've seen people complaining about DRY, they're complaining about something unrelated to the original DRY principle.
I’m quite attracted to writing “good” code — in that I think about theory and principles a lot — and “DRY” is not part of my vocabulary. I very much agree with your point that over-engineering is a problem but DRY is not related to that.
I wouldn't call this over engineering. I call it laziness to learn and refactor.
I get the point of OO and see how it's supposed to be used, and how handy it would be in large systems, but in my world the information hiding has been more of a hindrance than a help. Maybe what I really want to say is that it's harder than people think to do OO well, and when it's not done well, it just make everything more buggy and complicated.
An exercise I often use is to pretend like whatever I'm writing needs to be usable on multiple projects, so it should be able to be a package that anybody could find useful. Then you work backwards from the interface you'd like a tool like this to have, and obfuscate anything other than the interface. Obviously you're not doing this for most business/controller logic, but for most other things it goes a long way towards avoiding the kind of spaghetti code the Law of Demeter seeks to weed out.
I violate my principles, but I violate your laws.