Why is this an issue?

A ternary expression packs both outcomes of a choice into a single line, so the reader takes in the condition and the two results at once. When the condition is negated, that reading no longer works in one pass: the reader has to invert the condition mentally before pairing each result with the case it belongs to. In order.status() != Status.SHIPPED ? "Pending" : "Shipped", the value that goes with "shipped" is the second one, even though shipping is what the condition talks about.

Flipping the condition and swapping the two results expresses exactly the same logic without that double negative. It also settles the ordering question in a consistent way: the positive case, which is usually the common one, comes first.

This rule raises an issue on a ternary expression whose condition is negated. A condition counts as negated when it is a ! expression such as !verbose or !list.isEmpty(), or a != comparison.

The rule is only about the readability of the two branches of a ternary. Negated conditions elsewhere, such as in if statements, are out of scope.

Exceptions

Null checks do not raise an issue. value != null ? value.name() : "unknown" reads naturally as a positive guard, "if there is a value", so inverting it would not help the reader.

How to fix it

Replace the condition with its positive form and swap the two result expressions, keeping each result with the case it handles. For !x, use x. For x != y, use x == y. For a negated call such as !list.isEmpty(), use list.isEmpty().

The rewritten expression must produce the same value as before, so make sure you move both results and not just the condition. Evaluation order matters when a result expression has a side effect or can throw, because only the selected branch is evaluated, and swapping the branches must not change which one that is for a given input.

If the negated form is genuinely the clearer one, for example because the negative case is the one the surrounding code exists to handle, this is a trade-off you may decide to keep rather than a false positive.

Code examples

Noncompliant code example

public class OrderFormatter {

  public String statusLabel(Order order) {
    return order.status() != Status.SHIPPED ? "Pending" : "Shipped"; // Noncompliant: negated condition
  }

  public String summarize(Order order, boolean verbose) {
    return !verbose ? order.id() : order.id() + " for " + order.customer(); // Noncompliant: negated condition
  }
}

Compliant solution

public class OrderFormatter {

  public String statusLabel(Order order) {
    return order.status() == Status.SHIPPED ? "Shipped" : "Pending";
  }

  public String summarize(Order order, boolean verbose) {
    return verbose ? order.id() + " for " + order.customer() : order.id();
  }
}

Resources

Related rules