Home

IF...ELSEIF constructs should end with ELSE clauses

Description

    The IF...ELSEIF code construct should always end with an ELSE clause. This ensures that all possible conditions are accounted for and that the code will execute properly. Without an ELSE clause, the code will not execute correctly if none of the IF or ELSEIF conditions are met. An ELSE clause provides a default action if none of the other conditions are met. This helps to ensure that the code is robust and can handle any situation.

Key Benefits

  • Clear code structure: Using an IF...ELSEIF construct with an ELSE clause ensures that the code is structured in a clear and organized manner, making it easier to read and debug.
  • No ambiguity: By including an ELSE clause, you can be sure that all possible cases are handled in the code, eliminating any ambiguity.
  • Efficient execution: By including an ELSE clause, the code can be executed more efficiently since the program does not have to check all possible cases.

 

Non-compliant Code Example

DECLARE flag BOOLEAN := TRUE;
BEGIN
  IF flag THEN                     
    DBMS_OUTPUT.PUT_LINE('flag is true!');
  ELSIF NOT flag THEN                   --Non compliant code (IF...ELSEIF clause without ELSE clause)
    DBMS_OUTPUT.PUT_LINE('flag is false!');
  END IF;
END;

Compliant Code Example

DECLARE flag BOOLEAN := TRUE;
BEGIN
  IF flag THEN                     
    DBMS_OUTPUT.PUT_LINE('flag is true!');
  ELSE THEN                     --Compliant code (IF...ELSEIF clause with ELSE clause)
    DBMS_OUTPUT.PUT_LINE('flag is false!');
  END IF;
END;
Visual Expert 2024
 VEPLSQLRULE145