优化以下代码: for (DqExecuteResult dqExecuteResult : dqExecuteResults) { // 一致性 if (RuleTemplateName.MULTI_TABLE_ACCURACY.getCode().equals(dqExecuteResult.getRuleName())) { coherenceTotal += dqExecuteResult.getTotal(); if (DqTaskState.FAILURE.getCode() == dqExecuteResult.getState()) { coherenceSuccess += (dqExecuteResult.getTotal() - Math.round(dqExecuteResult.getStatisticsValue())); } else{ coherenceSuccess += dqExecuteResult.getTotal(); } } // 完备性 else if(RuleTemplateName.NULL_CHECK.getCode().equals(dqExecuteResult.getRuleName())){ completeTotal += dqExecuteResult.getTotal(); if (DqTaskState.FAILURE.getCode() == dqExecuteResult.getState()) { completeSuccess += (dqExecuteResult.getTotal() - Math.round(dqExecuteResult.getStatisticsValue())); } else{ completeSuccess += dqExecuteResult.getTotal(); } } // 有效性 else { effectiveTotal += dqExecuteResult.getTotal(); if (DqTaskState.FAILURE.getCode() == dqExecuteResult.getState()) { effectiveSuccess += (dqExecuteResult.getTotal() - Math.round(dqExecuteResult.getStatisticsValue())); } else{ effectiveSuccess += dqExecuteResult.getTotal(); } } }
时间: 2024-02-09 18:12:19 浏览: 73
可以考虑以下优化:
1. 将重复的代码提取出来,避免重复书写,可以提高代码的可读性和维护性;
2. 可以使用 switch-case 语句替代 if-else if 语句,使代码更加简洁;
3. 可以将常量提取出来,避免在代码中多次出现,提高可维护性。
下面是优化后的代码:
```
final String MULTI_TABLE_ACCURACY = RuleTemplateName.MULTI_TABLE_ACCURACY.getCode();
final String NULL_CHECK = RuleTemplateName.NULL_CHECK.getCode();
for (DqExecuteResult dqExecuteResult : dqExecuteResults) {
String ruleName = dqExecuteResult.getRuleName();
int state = dqExecuteResult.getState();
int total = dqExecuteResult.getTotal();
double statisticsValue = dqExecuteResult.getStatisticsValue();
switch (ruleName) {
case MULTI_TABLE_ACCURACY:
coherenceTotal += total;
coherenceSuccess += (state == DqTaskState.FAILURE.getCode()) ? (total - Math.round(statisticsValue)) : total;
break;
case NULL_CHECK:
completeTotal += total;
completeSuccess += (state == DqTaskState.FAILURE.getCode()) ? (total - Math.round(statisticsValue)) : total;
break;
default:
effectiveTotal += total;
effectiveSuccess += (state == DqTaskState.FAILURE.getCode()) ? (total - Math.round(statisticsValue)) : total;
}
}
```
阅读全文