Return to site

CODE SMELL: Middle man ๐Ÿ‘ค๐Ÿ‘จ๐Ÿ‘ค

January 7, 2023

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.