Search code examples
javasonarqubesonarlint

Sonar lint : "compareTo" results should not be checked for specific values


I am compare two values and getting sonar lint throwing "Only the sign of the result should be examined" this issue.

Code :

if (recBalanceAmt.compareTo(recRolloverEligibility) == 1) {
     recExpAmt = recBalanceAmt.subtract(recRolloverEligibility);
}

How to resolve this issue?


Solution

  • Sonar is suggesting to check the result of compareTo against 0, not if it returns directly 1, -1.

    if (recBalanceAmt.compareTo(recRolloverEligibility) > 0) {
    

    You can find the reason for this suggestion in the compareTo() Javadoc

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