Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Message Chains" vs "Middle Man"

Tags:

refactoring

I'm reading through Fowler's refactoring book and am a bit confused about those two code smells.

"Message Chains" are calls like a.getB().getC().getValue().

"Middle Man" is a method like

class A
{
    object getCValue()
    {
        return b.getCValue();
    }
}

The way I understand the two section is that if you've got "Message Chains" you shorten them by introducing "Middle Men". And If you've got "Middle Men" you turn them into "Message Chains"...

Now obviously there has to be some limitation to this or you'd have a programmer's merry-go-round. At what point should I favor one over the other?

One is coupling the class to unrelated classes, the other is coupling the class to the structure. So in theory my approach would be to check if any given change reduces one kind of coupling more than it increases the other kind of coupling. But is one kind of coupling worse and should be weighed more? I.e. only add one class coupling if you can remove X structural couplings?

like image 300
Kempeth Avatar asked Jul 07 '11 10:07

Kempeth


People also ask

What is message chains?

A message chain occurs when a client requests another object, that object requests yet another one, and so on. These chains mean that the client is dependent on navigation along the class structure. Any changes in these relationships require modifying the client.

Which object oriented design principle do long message chains a code smell usually violate?

Long message chains make our systems rigid and harder to test independently. It usually also violates the Law of Demeter, which specifies which methods are allowed to be called for a good object-oriented design.

What is middleman smell code?

A Middle Man is a class that in responsible, principally, for delegation. A class with a Middle Man smell increases the complexity of the code without contributing the program's functionality, as well as allows code to avoid logical flows of data based the relationship between classes.

What is inappropriate intimacy?

InappropriateIntimacy is a CodeSmell that describes a method that has too much intimate knowledge of another class or method's inner workings, inner data, etc.


1 Answers

Preferring middle men over message chains is also known as the Law of Demeter, which can be summarized as "only talk to your direct dependencies".

One benefit of using middle men instead of message chains is that you have to provide fewer mocks when doing unit testing. Classes become really hard to test when you have to provide mocks not only for their direct dependencies but also their indirect ones.

It also helps with separation of concerns, as code that has an A and wants a C should not have to know that there's a B involved. This helps modularity.

The main argument for message chains is that you don't have to write the boilerplate in the middle, and it might make sense in some cases, but I think the rule of thumb should be to prefer middle men.

like image 169
hammar Avatar answered Oct 05 '22 02:10

hammar