Return to site

CODE SMELL: Middle man 👤👨👤

· code-smell,coding

The middle man is a class that has several methods which act as passthrough 🛂 methods (they simply delegate the call to a method of one of the class' dependencies), without adding ❌➕ any meaningful logic on top of it.

 

Why is it bad? 👎

This pattern adds virtually no value ❌💰 to the code, but it introduces additional sources of coupling 🔁 since we are calling methods between multiple interfaces, and often fooling ourselves that we are following the information-hiding 🙈 principle (if the methods of a class are calling other methods of another class, is this really information hiding? 🤔)

Generally, encapsulation and information hiding 🙈 are highly encouraged 🙌, but when methods become too shallow ❌💪, it's a sign that we are not properly structuring our code.

 

How to fix it? 💡

If there is no additional logic, remove 🧹 as many intermediate as possible. Keep in mind 🧠 the separation between layers (if however, layers are existing just to pass methods to lower-level layers, the system architecture probably needs some rework).

Combine methods and simplify 👌 the class interface to provide more functionality through fewer methods.