Middle Man Code Smell Resolution with examples

Middle Man Code Smell Resolution with examples

Today in this article, we will talk about Middle Man Code Smell Resolution with examples.

This code smell more talks about encapsulation. As we know there are many advantages of encapsulating the delegated objects and exposing the delegates directly.

“Code is maintained more often than it is written”

Middle Man code smells come under a category called ‘Couplers’. In our last article, we understood couplers with the smell of Inappropriate Intimacy. and Feature envy which talks about class relationships and features/functions/method coupling, etc.

“Code is read more often than it is written”

Today in this session we will talk about the encapsulation of delegates.

Remove Middle Man

This is one of the code smell recipes to fix the code smell associated with delegated objects.

Let’s take an example with code smell,

Code Smell examples

   class Program
    {
       static void Main(string[] args)
        {
            Person person = new Person();
            person = person.GetManager();
        }
    }


    class Person
    {
        public Department Department { get; set; }
        public Person GetManager()
        {
            return Department.GetManager();
        }
    }


    class Department
    {
        private readonly Person _manager;
        public Department(Person manager)
        {
            _manager = manager;
        }
        public Person GetManager()
        {
            return _manager;
        }
    }

As in the above example, the Client class calls the Person’s delegated object to call the Department and get the manager’s details.

Awesome Code Middle Man Code Smell Resolution with examples

The issue with this approach is that every time the client wants to use the new feature of the delegates, the same feature needs to be added to the Person and Department as well.

To remediate this issue one can use a recipe called “Remove Middle Man”.

Resolution

Let’s remove the Middle man and expose the delegates from both Person and department to the client directly. So the client talks and knows the object it calling.

class Program
    {
        static void Main(string[] args)
        {
            Person person = new Person();
            Department department = person.GetDepartment();
            person = department.GetManager();
        }
    }
    class Person
    {
        public Department Department;
        public Department GetDepartment()
        {
            return Department;
        }
 
    }

    class Department
    {
        private readonly Person _manager;
        public Department(Person manager)
        {
            _manager = manager;
        }
        public Person GetManager()
        {
            return _manager;
        }

    }

The client talks to the Person and the Department objects directly. This removes the middleman or encapsulation. Also now the client talks and knows who is he interacting with.

Awesome Code Middle Man Code Smell Resolution with examples

Happy Coding !!

References:

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *