Middle Man

Keywords: middle man, code ai

Middle Man is a code smell where a class delegates the majority of its method calls directly to another class without performing any meaningful logic of its own — functioning as a pure passthrough that adds a layer of indirection without adding abstraction, transformation, error handling, or any other value, violating the principle that every layer in a software architecture must earn its existence by contributing something to the system.

What Is Middle Man?

Middle Man is the opposite of Feature Envy — instead of a class's methods reaching into another class to use its data, Middle Man is a class that hands all requests to another class without doing any work itself:

``python
# Middle Man: DepartmentManager adds zero value
class DepartmentManager:
def __init__(self, department):
self.department = department

def get_employee_count(self):
return self.department.get_employee_count() # Pure delegation

def get_budget(self):
return self.department.get_budget() # Pure delegation

def add_employee(self, emp):
return self.department.add_employee(emp) # Pure delegation

def get_head(self):
return self.department.get_head() # Pure delegation

# Better: Access department directly, or create a meaningful wrapper
`

Why Middle Man Matters

- Indirection Without Value: Every added layer of indirection has a cost — the developer must trace through it to understand what is actually happening. Middle Man imposes this cost while providing no compensating benefit: no abstraction, no error handling, no transformation, no caching, no logging. Pure overhead.
- Debugging Complexity: Stack traces that pass through Middle Man classes are longer, more confusing, and harder to parse. A bug that manifests inside
Department appears three levels deep in a trace that passes through DepartmentManager.add_employee()department.add_employee() → crash. The extra frame adds confusion without adding context.
- Change Propagation: When the underlying class changes its interface, the Middle Man must be updated to match — adding maintenance work for no structural benefit. If
Department adds parameters to add_employee(), DepartmentManager` must be updated identically.
- False Encapsulation: Middle Man can create the appearance that direct access to the underlying class is being avoided, suggesting an abstraction boundary that does not meaningfully exist. This misleads architectural understanding.
- Testability Illusion: Middle Man creates the appearance that tests cover a "layer" when they are actually testing pure delegation — the tests provide false confidence about coverage without testing any actual logic.

Middle Man vs. Legitimate Patterns

Not all delegation is Middle Man. Several legitimate patterns involve delegation:

| Pattern | Why It Is NOT Middle Man |
|---------|--------------------------|
| Facade | Simplifies complex subsystem — aggregates multiple objects, provides a simpler interface |
| Proxy | Adds access control, caching, logging, or lazy initialization |
| Decorator | Adds behavior before/after delegation |
| Strategy | Selects between different implementations based on context |
| Adapter | Translates between incompatible interfaces |

The key distinction: legitimate delegation patterns add something (simplification, behavior, translation). Middle Man adds nothing.

Refactoring: Remove Middle Man

The standard fix is direct access — eliminate the passthrough:

1. For each Middle Man method, identify the underlying delegated method.
2. Replace all calls to the Middle Man method with direct calls to the underlying class.
3. Remove the Middle Man methods.
4. If the Middle Man class becomes empty, delete it.

When the delegation is partial (some methods delegate, some add logic), use Inline Method selectively — inline only the pure delegation methods and keep the methods that add value.

Tools

- JDeodorant (Java/Eclipse): Identifies Middle Man classes and suggests Remove Middle Man refactoring.
- SonarQube: Detects classes where the majority of methods are pure delegation.
- IntelliJ IDEA: "Method can be inlined" suggestions identify delegation chains.
- Designite: Design smell detection covering delegation anti-patterns.

Middle Man is bureaucracy in code — an unnecessary administrative layer that routes requests without processing them, imposing comprehension overhead and maintenance burden on every developer who must navigate through it while contributing nothing to the correctness, reliability, or clarity of the system it inhabits.

Want to learn more?

Search 13,225+ semiconductor and AI topics or chat with our AI assistant.

Search Topics Chat with CFSGPT