I have the following code which iterates on an Integer[]
and makes sure that the the values in the array are in ascending order and that there are no neighbouring duplicates:
Integer previous = null;
for (Integer tb : trancheBoundaries) {
if (previous == tb) {
throw new IllegalThresholdConfigurationException("Two boundaries are the same");
} else if (previous != null && previous > tb) {
throw new IllegalThresholdConfigurationException("Previous is larger than current - tranche boundaries must be in ascending order");
} else {
previous = tb;
}
}
I am getting a warning from Sonar warning of "suspicious comparison of integer references" on the first if
clause.
I know why I am getting this, but in a situation like this, is it bad practice? I think it's ok because I know that I will always be comparing the same instances.
Objects.equals
to avoid causing confusion, following "the principle of least surprise". \$\endgroup\$