Skip to content

Simple symmetric flow of control

Reinhard Budde edited this page Apr 16, 2019 · 1 revision

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 one return at the end of the method. I prefer one return.
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;
}
Clone this wiki locally