Home

Boolean checks should not be inverted

Description

    The rule "Boolean checks should not be inverted" states that Boolean checks should not be written in a way that reverses the logic of the check. This means that the code should not use "not" or "!" to invert the result of a Boolean check. Instead, the code should be written in a way that directly expresses the logic of the check.

Key Benefits

  • Eliminates Errors: By not inverting Boolean checks, errors can be eliminated as the code is more readable and easier to debug.
  • Improves Performance: By not inverting Boolean checks, the code runs faster as the compiler can optimize the code better.
  • Reduces Complexity: By not inverting Boolean checks, the code is more straightforward and easier to understand.

 

Non-compliant Code Example

BEGIN

   IF NOT salary >= 20000 THEN       --Non compliant code (IF condition is having inverted boolean check)
      ILevel := 'Average Salary';

   ELSIF (NOT (salary < 20000)) and (NOT (salary >= 60000)) THEN  --Non compliant code (ELSE IF condition is having inverted boolean check)
      ILevel := 'Average Salary'; 

   ELSIF salary > 6000 and salary <= 110000 THEN
      ILevel := 'Moderate Salary';

   ELSE
      ILevel := 'High Salary';

   END IF;

END;

Compliant Code Example

BEGIN

   IF salary <= 20000 THEN      --Compliant code
      ILevel := 'Average Salary';

   ELSIF salary > 20000 and salary <= 60000 THEN --Compliant code
      ILevel := 'Average Salary'; 

   ELSIF salary > 6000 and salary <= 110000 THEN
      ILevel := 'Moderate Salary';

   ELSE
      ILevel := 'High Salary';

   END IF;

END;
Visual Expert 2024
 VEPLSQLRULE123