After reading
What is a" feature envy" code and why is it considered a code smell?"
I know the following code is suffering from "feature envy":
public class Garden{
public float width;
public float height;
}
public class Owner{
Garden garden;
public float getAddLawnCost(){
return this.garden.width*this.garden.height*LAWN_PRICE_PER_1M2;
}
}
and area of garden should be calculated inside Garden:
public class Garden{
private float width;
private float height;
public float getArea(){
this.width*this.height;
}
}
public class Owner{
Garden garden;
public float getAddLawnCost(){
return this.garden.getArea()*LAWN_PRICE_PER_1M2;
}
}
Suddenly a new requirement is added : calculate the cost of adding fence to the garden:
"Feature envy" version, class to modify : Owner
public class Garden{
public float width;
public float height;
}
public class Owner{
Garden garden;
public float getAddLawnCost(){
return this.garden.width*this.garden.height*LAWN_PRICE_PER_1M2;
}
public float getAddFenceCost(){
return (this.garden.width+this.garden.height)*2*FENCE_PRICE_PER_1M;
}
}
"non-feature envy" version, classes to modify : Owner, Garden:
public class Garden{
private float width;
private float height;
public float getArea(){
this.width*this.height;
}
public float getPerimeter(){
(this.width+this.height)*2;
}
}
public class Owner{
Garden garden;
public float getAddLawnCost(){
return this.garden.getArea()*LAWN_PRICE_PER_1M2;
}
public float getAddFenceCost(){
return this.garden.getPerimeter()*FENCE_PRICE_PER_1M;
}
}
111
Which the "non-feature envy" version has one extra class to modify : Garden, and hence violating "Open closed principle". Is it true?