Home

Related IF / ELSIF statements and WHEN clauses in a CASE should not have the same condition

Description

    The rule related to IF / ELSIF statements and WHEN clauses in a CASE states that they should not have the same condition. This means that the conditions used to evaluate the IF / ELSIF statements and WHEN clauses should be distinct from each other. This ensures that the CASE statement is properly evaluated and that the correct result is returned. Having the same condition for both the IF / ELSIF statements and WHEN clauses can lead to unexpected results and should be avoided.

Key Benefits

  • Efficient Code: Using related IF/ELSIF statements and WHEN clauses in a CASE with different conditions ensures that the code is efficient and optimized.
  • Reduced Complexity: Having different conditions for related IF/ELSIF statements and WHEN clauses in a CASE reduces the complexity of the code.
  • Improved Readability: Using different conditions for related IF/ELSIF statements and WHEN clauses in a CASE makes the code more readable and easier to understand.

 

Non-compliant Code Example

DECLARE flag BOOLEAN := TRUE;
BEGIN

  CASE
    WHEN flag THEN
		DBMS_OUTPUT.PUT_LINE('flag is false!');
	WHEN flag THEN                          --Non compliant code (When clause in the Case having the same condition)
		DBMS_OUTPUT.PUT_LINE('flag is true!');
	
  END CASE;

IF counter > 0 THEN
		cust_order := 1;
		message := 'Number is positive';
	ELSIF counter > 0 THEN            --Non compliant code (When clause in the Case having the same condition)
		cust_order := 2;
		message := 'Number is positive';
	ELSE
		cust_order := 0;
		message := 'Number is negative';
	END IF;

END;

Compliant Code Example

DECLARE flag BOOLEAN := TRUE;
BEGIN

  CASE
    WHEN NOT flag THEN
		DBMS_OUTPUT.PUT_LINE('flag is false!');
	WHEN flag THEN                          --Compliant code
		DBMS_OUTPUT.PUT_LINE('flag is true!');
	
  END CASE;
END;
Visual Expert 2024
 VEPLSQLRULE59