What is Feature Envy?

Asked

Viewed 780 times

11

What is Feature Envy?

Why is it considered a bad smell of code (code Smell)?

What are the advantages of avoiding it?

She must always be avoided?

1 answer

10


One method can access data of another type to do some operation. When this becomes common it means that the object itself should perform this operation and deliver the result. So it is said that one method was left with envy of a kind and wanted to do it on his own.

There is no way to avoid it always because it depends on changing the original type that you don’t have access to or can’t change it for some reason. There are cases where behavior needs to be purposely separated.

The idea is that it violates encapsulation and is not so object-oriented. Since not everything needs this, even when it can, it is not something that needs to be avoided forever. So much so that this is one of the cases where you have to choose which poison to take, because filling a kind of methods to try to solve everything that may appear or leaving the type open for change can be an even bigger problem. It is common to have another Smell that says the fix is to do exactly what this says it can’t.

There are types that only exist to provide data. This causes chills in many people, but used in the right thing is a good solution. Just as it’s better to have utility classes or some famous design patterns that make up to perform certain tasks that should not be handled by type.

It depends a lot on the case how much it has advantage, in some cases the advantage is at least debatable.

I have the opinion that this is one of the classic examples of someone defining something by looking at detail and forgetting the whole. It’s another of the things created by theorists.

Yes, all this is not OO, how nice.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.