Law of Demeter (Principle of Least Knowledge)

I was reading up on the Law of Demeter again after reading Jeremy Miller’s post about orthogonality (good post by the way).

The Law of Demeter (LoD from now on) was created at NorthEastern University, Boston in 1987 while a team was working on the Demeter project.
LoD dictates that object A, when calling object B should call a service on object C through B.


Example:

Bad (Common):
inventory.SalesInfo.Items.Count = 2500;

Good:
SalesInfo si = inventory.SalesInfo;
int salesItemsCount = si.GetSalesItemsCount();

[granted this might possibly be a violation of Single Responsibility Principle (SRP)  - Thanks Josh (Joshua Lockwood)]


I’m definitely guilty of violating this “law”.  I know via reading
and experience that this idea does help decouple your code.  You are
able to re-use and place your code under test coverage more easily.

LoD brings out the basic OO principle of encapsulation, hence the
name “Principle of Least Knowledge”.  With our current IDEs out there
today it is very easy to forget about not walking a list of dots.  What
I mean is, if you have more than 1 dot in your call (i.e.,
person.spouse.name (bad) instead of person.GetSpouseName() (good)) you
are most likely violating LoD.

An idea that Deane had on his post about LoD (hist first comment),
was to create a tool that would walk a developer’s code and let the
developer know if they have violated LoD.  I can see writing something
like this using reflection in Java/.NET.  An idea for the future maybe.

Two books to read about this:
Code Complete (2nd Ed.) by Steve McConnell
Pragmatic Programmer by Thomas, Hunt

[If you look at the first amazon link you can buy both books together for ~$66 - very much worth it]

Related Articles:

Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Jason Meridth

Continuously learning software developer trying to not let best be the enemy of better
This entry was posted in Programming. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

Comments are closed.