Course contentsShow
← Software Engineering
Lesson 344 of 1,93310. RefactoringFree lesson

Couplers: Message Chains and Middle Man

Identifying excessive delegation chains and classes that only forward requests without adding value.

Couplers: Message Chains and Middle Man

You'll learn to spot two coupling smells: code that navigates through long object chains and classes that do nothing but pass requests along.

Message Chains

Message chains occur when you see code like user.getAccount().getProfile().getSettings().getTheme(). Each method call depends on the previous one, creating a fragile chain. If any link in the chain changes (say, getProfile() returns null), your code breaks.

Analogy: Imagine asking your neighbor to ask their neighbor to ask their neighbor for a cup of sugar. It's inefficient and brittle—what if someone moves?

This smell violates the Law of Demeter (principle of least knowledge): an object should only talk to its immediate friends, not friends-of-friends-of-friends. Message chains create tight coupling because the calling code must know the entire structure of intermediate objects.

Middle Man

The opposite problem is a class that exists solely to delegate work to another class. If most of a class's methods just call the same method on another object, you have a Middle Man.

Analogy: A manager who just forwards every question to their team without adding value—why not talk to the team directly?

While delegation is often good (it's used in patterns like Proxy and Decorator), a Middle Man adds complexity without benefit. It creates an unnecessary layer that makes code harder to navigate.

The Balance

Both smells involve excessive indirection. Message chains have too little delegation (reaching through multiple objects), while Middle Man has too much pointless delegation. The solution? Message chains need extracted methods or direct relationships; Middle Men should be removed or given real responsibilities.

Key Takeaway: Watch for long chains of method calls reaching through objects, and classes that only forward requests—both create coupling without adding value.