{
    "slug": "middle_man",
    "term": "Middle Man",
    "category": "quality",
    "difficulty": "intermediate",
    "short": "A class that does little more than delegate every method to another object — an unnecessary layer of indirection.",
    "long": "The Middle Man smell occurs when a class primarily exists to forward calls to another class, offering no additional logic or value. It arises from over-application of delegation or from classes that were once more substantial but had their behaviour stripped out. The remedy is typically to remove the intermediary and call the delegate directly (inline delegation), or if the class is a legitimate abstraction, to add real behaviour to justify its existence.",
    "aliases": [
        "middle man smell",
        "over-delegation",
        "pass-through class"
    ],
    "tags": [
        "code-smell",
        "refactoring",
        "oop"
    ],
    "misconception": "Delegation is always better than direct access. When a class does nothing but delegate every call to another, it adds indirection without value — callers may as well use the delegate directly, and the middle man should be collapsed.",
    "why_it_matters": "A class that does nothing but delegate to another class adds a navigation layer with no value — callers must understand two classes to achieve what one would do.",
    "common_mistakes": [
        "Service classes that are pure pass-throughs to a repository with no added logic.",
        "Facade classes that expose every method of the subsystem 1:1 without simplification.",
        "Not removing middle men during refactoring — they accumulate when classes are extracted but the original is never deleted.",
        "Over-using delegation patterns from DDD without checking whether each layer adds value."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "feature_envy",
        "law_of_demeter",
        "god_class"
    ],
    "prerequisites": [
        "single_responsibility",
        "refactoring",
        "coupling"
    ],
    "refs": [
        "https://refactoring.guru/smells/middle-man"
    ],
    "bad_code": "// UserManager just delegates every call — pointless layer\nclass UserManager {\n    public function __construct(private UserRepository \\$repo) {}\n    public function find(int \\$id): ?User          { return \\$this->repo->find(\\$id); }\n    public function save(User \\$u): void           { \\$this->repo->save(\\$u); }\n    public function delete(int \\$id): void         { \\$this->repo->delete(\\$id); }\n}",
    "good_code": "// Remove the middle man — inject UserRepository directly where needed\nclass UserController {\n    public function __construct(private UserRepository \\$users) {}\n    // use \\$this->users directly — no useless delegation layer\n}",
    "example_note": "If a class does nothing but forward calls to another, delete it and inject the dependency directly.",
    "quick_fix": "Remove a class that only delegates to another — inline the delegation and let callers call the real class directly, or enhance the wrapper to add meaningful value",
    "severity": "low",
    "effort": "medium",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/middle_man",
        "html_url": "https://codeclaritylab.com/glossary/middle_man",
        "json_url": "https://codeclaritylab.com/glossary/middle_man.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Middle Man](https://codeclaritylab.com/glossary/middle_man) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/middle_man"
            }
        }
    }
}