When a single primitive is tested against three or more values in an IF
, ELSIF
chain, it should be converted to a
CASE
instead for greater readability.
Noncompliant code example
DECLARE
x PLS_INTEGER := 0;
BEGIN
IF x = 0 THEN -- Noncompliant
DBMS_OUTPUT.PUT_LINE('x = 0');
ELSIF x = 1 THEN
DBMS_OUTPUT.PUT_LINE('x = 1');
ELSE
DBMS_OUTPUT.PUT_LINE('x > 1');
END IF;
END;
/
Compliant solution
DECLARE
x PLS_INTEGER := 0;
BEGIN
CASE x
WHEN 0 THEN
DBMS_OUTPUT.PUT_LINE('x = 0');
WHEN 1 THEN
DBMS_OUTPUT.PUT_LINE('x = 1');
ELSE
DBMS_OUTPUT.PUT_LINE('x > 1');
END CASE;
END;
/