When a method accesses the data of another object more than its own data, like in below java example, we have a code smell called feature envy:

float costOfPainting(Wall wall, Paint paint) {
	float area = wall.width() * wall.height();
	float litresPerSquareMeter = paint.efficiency();
	float litres = litresPerSquareMeter * area;
	return litres * paint.pricePerLitre();
}

The remedy is to extract and move parts of that method:

float costOfPainting(Wall wall, Paint paint) {
	return wall.area() * paint.pricePerSquareMeter();
}

It's one of the principles of object oriented programming – keep the data and methods that manipulate the data together in the same object.

A method that extensively accesses the data of another object in order to perform some sort of computation or make a decision, should rather ask the object to do the computation itself. This simplifies code that is using this object, making it more readable.

M. Fowler, Refactoring

            Votes: 0

See more like this: