我有一个需要降低圈复杂度的方法。但我不确定如何去做。
public boolean isEqual(Car car) {
if (this.id != car.id) return false;
if (this.reg != car.reg) return false;
if (this.vin != car.vin) return false;
if (this.make != car.make) return false;
if (this.model != car.model) return false;
return true;
}
方法中还有更多类似的 if 语句,但我只包含了一些。如何降低复杂性? 抱歉,如果格式不正确,因为我是在手机上写的。
最佳答案
确实没有办法让它变得那么干净。正如 Maroun 在评论中质疑的那样,实际上并没有那么复杂。但是,您可以去掉“if”语句,这样阅读起来会更容易一些:
public boolean isEqual(Car car) {
return this.Id == car.id &&
this.reg == car.reg &&
this.vin == car.vin &&
this.make == car.make &&
this.model == car.model;
}
您可以根据需要继续添加。这将有点像你的“if”语句,因为当它发现“假”结果时,它将停止检查任何其他结果,而只返回错误结果。
关于java - 降低包含大量 if 语句的 if 语句的复杂性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28727564/