我遇到声纳违规:
“条件不应无条件评估为“TRUE”或“FALSE””
对于下面的代码。
List<MediaContent> savedList = source.getChildMediaContents();
List<MediaContent> supplierList = target.getChildMediaContents();
// if existing and incoming both empty
if(savedList == null && supplierList == null){
return false;
}
// if one is null and other is not then update is required
if(savedList == null && supplierList != null){
return true;
}
if(savedList != null && supplierList == null){
return true;
}
在两个 if 块下方它给出了错误
// if one is null and other is not then update is required
if(savedList == null && supplierList != null){
return true;
}
if(savedList != null && supplierList == null){
return true;
}
if(savedList == null && supplierList == null){
return false;
}
if(savedList == null && supplierList != null){
条件supplierList != null
达到时始终为真。
由于短路行为&&
Java 中的运算符,
前supplierList != null
到达了,savedList == null
首先必须是真的。
But if savedList == null
是真的,
那么从前面的条件我们知道supplierList
is not null
,所以这是一个毫无意义的条件。
另一方面,如果savedList == null
是假的,
那么由于短路行为,
这supplierList != null
不会被评估。
因此,无论结果如何savedList == null
,
supplierList != null
永远不会被评价,
所以你可以简单地删除这个条件。
if (savedList == null) {
return true;
}
Next:
if(savedList != null && supplierList == null){
由于之前的简化,现在很清楚savedList
不可能是null
。所以我们也可以删除这个条件:
if (supplierList == null) {
return true;
}
简而言之,这相当于您发布的代码:
if (savedList == null && supplierList == null) {
return false;
}
if (savedList == null || supplierList == null) {
return true;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)