Sunday, October 18, 2009

Law of Demeter and violations.

Law of Demeter - "Only talk to your immediate friends"
An object should avoid invoking methods of a member object returned by another method. In Java,the law can be stated simply as "use only one dot". That is, the code "a.b.Method()" breaks the law where "a.Method()" does not.

Violations:
With the SOA applications rising up these days, its very difficult to draw a line to what extend one can violate this law.

One way to create appropriate facades to wrap the internal extensions, however it just looks like for the sack of the (LoD) Law of Demeter and in fact slows one down on development.

Lot of third party libraries does not seem to care about the principle at all.Everyday we use these libraries,needless to say with lot of dots,and when we get "NullPointerException", the deguggin goes crazy.

We should realize the value of LoD, at least from unit testing perspective and its very hard to test the piece of the code that violates the LoD to larger extend.

Strongly feel, developers or API Architects should take LoD into account when they design plugin/libraries.

No comments: