Simple symmetric flow of control - OpenRoberta/openroberta-lab GitHub Wiki
Sonar complains about this structure. It is used many times.
And Sonar is correct about this:
_the flow of control is hard to understand and dangerous._
public Void visitXxx(Xxx<Void> xxx) {
if ( !xxx.getMode().toString().equals(BlocklyConstants.DEFAULT) ) {
...many-lines-1...
} else {
if ( xxx.getRgbLedColor().getClass().equals(ColorConst.class) ) {
...many-lines-2...
return null;
}
if ( xxx.getRgbLedColor().getClass().equals(Var.class) ) {
...many-lines-3...
return null;
}
...many-lines-4...
}
return null;
}
There are different solutions to this. I propose one. Any solution should
- show the symmetry of the
...many-lines-x...
. All are on the same level - have a
return
at any branch OR have onereturn
at the end of the method. I prefer onereturn
.
public Void visitXxx(Xxx<Void> xxx) {
if ( !xxx.getMode().toString().equals(BlocklyConstants.DEFAULT) ) {
...many-lines-1...
} else if ( xxx.getRgbLedColor().getClass().equals(ColorConst.class) ) {
...many-lines-2...
} else if ( xxx.getRgbLedColor().getClass().equals(Var.class) ) {
...many-lines-3...
} else {
...many-lines-4...
}
return null;
}