Archive for May, 2010

Law of Demeter

Sunday, May 9th, 2010

Recently I have read some articles that have mentioned the Law of Demeter, and not having heard of it, I went looking for it. Blatantly stolen from the article, the Law of Demeter is as follows:

A method “M” of an object “O” should invoke only the
the methods of the following kinds of objects:

  1. itself
  2. its parameters
  3. any objects it creates/instantiates
  4. its direct component objects

 Why is this a good thing? In simple terms, following the Law of Demeter, you enforce encapsulation of a method’s implementation and dependencies on other objects. Should that method have to change at a later time, it minimizes the impact on callers of the method, localizing the change to the method in question and its subordinates.

For more information, and perhaps a more succinct explanation, see the linked article above.

Refactoring Is Not Up To You

Sunday, May 9th, 2010

As a software contractor, and previous employee at many software shops, I have seen some amazingly well built code, and unfortunately my fair share of the horrible. To steal a quote from one of my favorite movies:

“I’ve seen things you people wouldn’t believe…”
 – Roy Batty, Nexus 6 Replicant, Blade Runner

I’ve seen the use of design patterns for the sake of having used a design pattern, with interfaces so generalized that types have been thrown out the window so as to refer to all parameters and return types as simply java.lang.Object. I’ve seen applications written in C++ where pointers to proper objects have been re-cast as unsigned long, totally stripping type information (and the fact that the value is a pointer), passed around and then re-cast to a (hopefully appropriate) object type elsewhere. I have seen Translator objects, written to perform translation between DTOs and general Beans meant to be consumed at the User Interface level, that also including translation from HTML form attributes to both DTOs and Beans. I’ve seen hard-coded character buffers in C/C++ applications that are easily overrun when std::string was readily available.

I have seen the Big Ball of Mud up-front and personal. (more…)