Inspectopedia Help

Conditional expression

Reports usages of the ternary condition operator and suggests converting them to if/else statements.

Some code standards prohibit the use of the condition operator.

Example:

Object result = (condition) ? foo() : bar();

After the quick-fix is applied:

Object result; if (condition) { comp = foo(); } else { comp = bar(); }

Configure the inspection:

Use the Ignore for simple assignments and returns option to ignore simple assignments and returns and allow the following constructs:

String s = (foo == null) ? "" : foo.toString();

Use the Ignore places where an if statement is not possible option to ignore conditional expressions in contexts in which automatic replacement with an if statement is not possible (for example, when the conditional expression is used as an argument to a super() constructor call).

Inspection options

Option

Type

Default

Ignore for simple assignments and returns

Checkbox

false

Ignore places where an if statement is not possible

Checkbox

true

Inspection Details

Available in:

IntelliJ IDEA 2023.3, Qodana for JVM 2023.3

Plugin:

Java, 233.SNAPSHOT

Last modified: 13 July 2023