The conditional operator should be used in correct order

  • BAD_CONDITIONAL_OPERATOR
  • Error
  • Medium
  • No tags

This rule applies when the conditional expression of conditional operator is ambiguous.

Within an expression, a conditional operator is calculated after other operators are calculated because its precedence is quite low.

Even if there is no fault in a conditional operator, it is recommended to apply parenthesis for readability and maintainability.

Noncompliant Code Example

View with compliant examples side by side
// Example 1
const msg = "Mode " + isActivated ? "activated" : "deactivated";

// Example 2
const size = base + (buf.length > MAX) ? MAX : buf.length;

Compliant Code Example

View with noncompliant examples side by side
// Example 1
const msg = "Mode " + (isActivated ? "activated" : "deactivated");
// ES6 or higher
const msg = `Mode ${isActivated ? "activated" : "deactivated"}`;

// Example 2
const size = base + ((buf.length > MAX) ? MAX : buf.length);

Version

This rule was introduced in DeepScan 1.8.0-beta.

See

Was this documentation helpful?