Why is this an issue?

The Comparable.compareTo method returns a negative integer, zero, or a positive integer to indicate whether the object is less than, equal to, or greater than the parameter. The sign of the return value or whether it is zero is what matters, not its magnitude.

Returning a positive or negative constant value other than the basic ones (-1, 0, or 1) provides no additional information to the caller. Moreover, it could potentially confuse code readers who are trying to understand its purpose.

How to fix it

Replace any positive constant return value with 1. Replace any negative constant return value with -1.

Code examples

Noncompliant code example

public int compareTo(Name name) {
  if (condition) {
    return Integer.MIN_VALUE; // Noncompliant
  }
}

Compliant solution

public int compareTo(Name name) {
  if (condition) {
    return -1; // Compliant
  }
}

Noncompliant code example

public int compareTo(Name name) {
  if (condition) {
    return 42; // Noncompliant
  }
}

Compliant solution

public int compareTo(Name name) {
  if (condition) {
    return 1; // Compliant
  }
}

Noncompliant code example

It is compliant to return other values than -1, 0 or 1 if they are not constants.

public int compareTo(Name name) {
  if (condition) {
    return 42; // Noncompliant
  }
}

Compliant solution

public int compareTo(Name name) {
  if (condition) {
    return hashCode() - name.hashCode(); // Compliant, not a constant
  }
}

Resources

Documentation