According to https://softwareengineering.stackexchange.com/a/212130/432039, if a class asks another class for the state, and then call methods of that class, it is called “feature envy”, eg:
if(a.state){
a.doSomething();
}
it should move the code into the object as an instance method:
public class A{
public void someMethod(){
if(this.state){
this.doSomething();
}
}
}
.
.
.
a.someMethod();
However, the answer only talks about the case that both the state and the action to take have only one “other class” to participate, what if there are other things that participate the state? eg:
if(a.state && b.state){
a.doSomething();
}
is it also a kind of feature envy and hence should modify as follows:
public class A{
public void someMethod(boolean bState){
if(this.state && bState){
this.doSomething();
}
}
}
.
.
.
a.someMethod(b.state);
? Or if other things also participate the action, eg:
if(a.state){
a.doSomething();
c.doSomething();
}
Should it modify as follows?
public class A{
public void someMethod(C c){
if(this.state){
this.doSomething();
c.doSomething();
}
}
}
.
.
.
a.someMethod();
And what if there are “else” actions to do, eg:
if(a.state){
a.doSomething();
}else{
d.doSomething();
}
? Should I refactor it? If so, how to refactor it?