I'm trying to toggle a class level boolean called toggle
like so:
public void myClass() {
private boolean toggle = false;
public void process(){
while (x < y) {
if (toggle()){
//do some stuff
} else {
//do some other stuff
}
}
}
private boolean toggle() {
return this.toggle = this.toggle ? false : true;
}
}
but SONAR complains about return this.toggle = this.toggle ? false : true;
saying "Inner assignments should be avoided." If i refactor this to pass the class level boolean into the toggle()
method it doesn't work and always returns true. Is there an elegant way of achieving the same without SONAR complaining?