Pergunta

In Martin Fowler's refactoring book, there is a code smell named "Insider Trading" which I don't understand:

Software people like strong walls between their modules and complain bitterly about how trading data around too much increases coupling. To make things work, some trade has to occur, but we need to reduce it to a minimum and keep it all above board.

Can you give me some code examples?

Foi útil?

Solução

Well, I don't have my copy of that book at hand, but let me make an educated guess what Fowler could have meant:

  • Let's say there is a function or module A which produces a list of strings or numbers. There is nothing in the documented contract of the public interface of A which guarantees a specific order of that list.

  • Now imagine a second function or module B which takes the list and does some lookup operations using binary search. Imagine B making the assumption that A always returns a sorted list. The author of B might know how A is implemented internally, and that A returns the list always in a sorted fashion. So B does not any sorting by itself.

That is what could be called "insider trading" - using undocumented internal implementation details ("insider knowledge") for shortcuts or simplifications. You can probably imagine what happens when a maintenance programmer who works on A, but knows nothing about B, has to change some internals in A, which leads to an output in a different order.

Outras dicas

I’m Mr. Alarm. My job is to flash red lights and make a terrible noise when something has gone wrong with the system. However, no one likes it when I go off for no good reason. When I do my designer gets yelled at.

My designer has me suppressing a lot of signals. The cpu is too hot! Oh but except when powering up or for brief periods or when it’s accessing GPU... and that’s just the CPU.

My designer has created a situation where I have to know almost everything about the whole system. No one can touch the system now without him.

It seems silly to me. All I want to know is when to go off. I’m real good at flashing lights and making noise. Why do I need to know this other stuff?

That’s insider trading. It’s about knowing too much.

Licenciado em: CC-BY-SA com atribuição
scroll top