Split Message Chain

Examine

What you’re looking for is code that has chained dependencies - it talks to a “friend’s friend”. A “friend” is a parameter of the current method, an object you constructed, or an attribute of the current class. Identify which object is the “friend” that you are ok to talk to, but that gives you access to other objects that you don’t want to depend on directly.

The unwanted dependency could be obvious if you have a clear Message Chain, like A.getB().getC(). In that example the “friend” is A, and the “friend’s friend” is the thing returned by “A.getB()”.

Prepare

If you don’t have a variable for the “friend” object, create one using Introduce variable.

Implement

Clear

Follow up

Prompted by Code Smells

Relevant Learning Hours

Sources

Back to All Refactorings