Inspectopedia Help

Conditional expression with negated condition

Reports conditional expressions whose conditions are negated.

Flipping the order of the conditional expression branches usually increases the clarity of such statements.

Use the Ignore '!= null' comparisons and Ignore '!= 0' comparisons options to ignore comparisons of the form obj != null or num != 0. Since obj != null effectively means "obj exists", the meaning of the whole expression does not involve any negation and is therefore easy to understand.

The same reasoning applies to num != 0 expressions, especially when using bit masks.

These forms have the added benefit of mentioning the interesting case first. In most cases, the value for the == null branch is null itself, like in the following examples:

static String getName(Person p) { return p != null ? p.getName() : null; } static String getExecutableString(int fileMode) { return (fileMode & 0b001001001) != 0 ? "executable" : "non-executable"; }

Inspection options

Option

Type

Default

Ignore '!= null' comparisons

Checkbox

true

Ignore '!= 0' comparisons

Checkbox

true

Inspection Details

Available in:

IntelliJ IDEA 2023.3, Qodana for JVM 2023.3

Plugin:

Java, 233.SNAPSHOT

Last modified: 13 July 2023