Shiladittya Chakraborty
Shiladittya Chakraborty

Reputation: 4418

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?

Upvotes: 5

Views: 4638

Answers (1)

guido
guido

Reputation: 19224

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.

Upvotes: 8

Related Questions