[英]How to fix Sonar issue “Remove this call to ”wait“ or move it into a ”while“ loop”?
[英]How to avoid sonar issue for multiple for loop
如何以优化方式修改以下代码以避免声纳问题?
if (CollectionUtils.isNotEmpty(customerVoList)) {
for (CustomerVO customerVO : customerVoList) {
if (CollectionUtils.isNotEmpty(customerVO.getCardVos())) {
for (CardVO cardVO : customerVO.getCardVos()) {
float amount = redemptionService.getFutureRedemableBalance(cardVO.getCardNo(), cardVO.getCurrentBalance());
cardVO.setCurrentBalance(BigDecimal.valueOf(amount));
}
}
}
}
声纳抛出此异常:
重构此代码,使其嵌套不超过 3 个 if/for/while/switch/try 语句
您可以通过删除这两个if
语句来简化此操作。 您可以让for
循环遍历一个空列表,这相当于跳过循环。
for (CustomerVO customerVO : customerVoList) {
for (CardVO cardVO : customerVO.getCardVos()) {
float amount = redemptionService.getFutureRedemableBalance(cardVO.getCardNo(), cardVO.getCurrentBalance());
cardVO.setCurrentBalance(BigDecimal.valueOf(amount));
}
}
您可以尝试以下技巧。
在其他一些方法中编写嵌套循环并使用参数调用该方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.