I am getting sonar violation:
"Conditions should not unconditionally evaluate to "TRUE" or to "FALSE""
for the code below.
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;
}
Below the two if blocks it is giving an error
// 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;
}
The condition
supplierList != nullis always true when reached. Due to the short-circuiting behavior of the&&operator in Java, beforesupplierList != nullis reached,savedList == nullmust be true first.But if
savedList == nullis true, then we know from the previous condition thatsupplierListis notnull, so it's a pointless condition.On the other hand, if
savedList == nullis false, then the due to the short-circuiting behavior, thesupplierList != nullwill not be evaluated.Thus, regardless of the outcome of
savedList == null,supplierList != nullwill never be evaluated, so you can simply remove that condition.Next:
Thanks to the simplification earlier, now it's clear that
savedListcannot benull. So we can remove that condition too:In short, this is equivalent to your posted code: