The logical negation operator should not be applied 3 or more times in a row

  • TRIPLE_NEGATION_OPERATOR
  • Code Quality
  • Low
  • No tags

This rule applies when the logical negation operator (!) is applied 3 or more times in a row.

A triple negation like !!!e is equivalent to the single negation !e.

Therefore, it is recommended to remove the unnecessary negation operations for readability and maintainability.

Noncompliant Code Example

View with compliant examples side by side
// Example 1
foo(!!!arg); // TRIPLE_NEGATION_OPERATOR alarm because 3 negations are used.

// Example 2
if ((cond1 && !(!!(cond2 && !cond3))) || cond4) { // TRIPLE_NEGATION_OPERATOR alarm because 3 negations are used.
    bar();
}

Compliant Code Example

View with noncompliant examples side by side
// Example 1
foo(!arg);

// Example 2
if ((cond1 && !(cond2 && !cond3)) || cond4) {
    bar();
}

Version

This rule was introduced in DeepScan 1.19.0.

Was this documentation helpful?