In Java with SonarQube, how to fix `Only the sign of the result should be examined`

Florian Castelain :

I have the following line of code (where "next" is a BigDecimal):

if (next.compareTo(maximum) == 1) {
    maximum = next;
}

On the equality comparison, SonarQube gives me this warning:

Only the sign of the result should be examined.sonarqube-inject

What does it actually mean and how can I fix that ?

keuleJ :

You should only test if it's greater than zero:

if (next.compareTo(maximum) > 0) {
                        maximum = next;
                    }

From the API docs of the compareTo Method:

Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=23148&siteId=1