Message chains
A message chain occurs when a client requests an object, which then requests another object, and so on. This makes all the objects dependent on the structure of the called objects, and a change in these objects would necessarily impact the entire chain of calls, at every point where they are made. Building upon the example from the previous section, we could have something like:
var cityName = person.getLocation().getAddress().getCity().getName();
The solution is to hide the “delegate”, that is the method calling the other one(s). We could transform the previous example into this:
var cityName = person.getCityName();
Where the chain of calls is hidden in the getCityName()
method.
Taken to the extreme, however, this refactoring can lead to the middle man smell! Have we ended up in a time loop like Christopher Nolan? The truth is, it depends. These refactorings must indeed be properly weighted, attempting to choose the lesser evil (also considering...