Law of Demeter: Only talk to your immediate friends ~ Don't Call Us - We'll Call You

Monday, February 23, 2009

Law of Demeter: Only talk to your immediate friends

dog.Body.Tail.Wag();

should really be:

dog.ExpressHappiness();

..and let the implementation of the dog decide what this means.

Peter Van Rooijen posted the following description of the Law Of Demeter to Usenet:
  • You can play with yourself.
  • You can play with your own toys (but you can't take them apart),
  • You can play with toys that were given to you.
  • And you can play with toys you've made yourself.

Wikipedia:
The Law of Demeter (LoD), or Principle of Least Knowledge, is a design guideline for developing software, particularly object-oriented programs. The guideline was invented at Northeastern University towards the end of 1987, and can be succinctly summarized as “Only talk to your immediate friends.” The fundamental notion is that a given object should assume as little as possible about the structure or properties of anything else (including its subcomponents).
C2 Wiki:
The LawOfDemeter makes UnitTests easier.

Miško Hevery:
Most applications tend to have some sort of Context object which is the kitchen sink and which can get you just about any other object in the system aka the service locator. [-] Writing tests for such code base sucks!
[....]
Always ask for things
-Abandon the new operator in 99% of your domain classes
-Move object creation to factories (or a DI container)

No comments:

Post a Comment