Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

sonarqube

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 ?

like image 735
Itération 122442 Avatar asked Sep 14 '25 19:09

Itération 122442


1 Answers

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.

like image 127
keuleJ Avatar answered Sep 17 '25 09:09

keuleJ