Kola Oyedeji just wrote a very nice article for the ColdFusion Developer's Journal on applying the Law of Demeter in ColdFusion object oriented programming (OOP). In his article, Kola summarizes that according to the Law of Demeter, an object should only call methods that:
- Belong to itself (i.e., its own methods)
- Are on objects passed in as a parameter
- Are on objects created by the object
As I learn more about object oriented programming (OOP), this makes a lot of sense to me. But, as Kola mentions, all laws/rules/best practices have a trade off. Strictly following this law might bloat your ColdFusion components with numerous methods that do little more than delegate calls to composed components. So the question I have is, where do you find that you most often break this law for the trade off of programming convenience?
The one example that he covers, a person's address, seems like a likely place to break this rule. I feel that people would rather do something like:
... rather than:
And imagine if we were dealing with two composed address objects: one for billing and one for shipping. The number of delegating methods could get out of hand. My gut feeling tells me that address is a solid enough component that it can have its methods called via dependency chains. But then again, I am not an object oriented ColdFusion programmer just yet.
So, where do you choose to violate this law?
Want to use code from this post? Check out the license.