我怎样才能减少这种功能的圈复杂度在Java?:如何降低java中的圈复杂度?
public void execute() {
if (dx == 1 && dy == 0) { this.moveRight();}
if (dx == -1 && dy == 0) { this.moveLeft();}
if (dx == 0 && dy == -1) { this.moveUp();}
if (dx == 0 && dy == 1) { this.moveDown();}
if (dx == 1 && dy == -1) { this.moveUpRight();}
if (dx == 1 && dy == 1) { this.moveRightDown();}
if (dx == -1 && dy == -1) { this.moveLeftUp();}
if (dx == -1 && dy == 1) { this.moveDownLeft();}
}
由于有8种方法,你需要有8个条件,这种排序是有道理的。顺便说一句,你可能想使用'if,else if'语句,因为程序是每当所有8个条件都被执行时 – nafas
'moveUpRight()'与moveUp()'+ moveRight()'相同吗? – Berger
我们不知道这些方法是做什么的。也许'moveUpRight'只是调用'moveUp(); moveRight();',也许不是。没有这一点,我看不出一种降低复杂性的方法。如果你问这个问题是因为像声纳这样的事情引发了一个警告,有时候可以忽略它。 – jhamon