Simberon Design Minute
 

Message Chains

One problem I often see in object oriented design is long message chains. They'll send myCustomer accountingInformation accountsPayable balance to get the balance of the customer's accounts payable account. The problem here is that at each step along the way, you're talking to a different object and you need to understand how to properly talk to that object to make it work. This makes your code much more tightly coupled. It moves responsibility away from the objects that should be doing the work causing duplication. If someone comes to your door selling chocolate bars you don't say "Sure, here's my wallet. Go take the right amount of money out and give it back to me". You should manage your own wallet, not give other objects access to it. This principle is called the Law of Demeter. In a nutshell, it says that objects can talk to their immediate neighbors, but not their neighbors' neighbors. This principle, when properly applied, tends to create better designs.

Download