Home
IF statements should not be nested too deeply
Description
The rule "IF statements should not be nested too deeply" in PL/SQL code means that when writing code, the programmer should avoid nesting multiple IF statements within each other. This is because deep nesting of IF statements can make the code difficult to read and understand, and can also lead to unexpected results. It is generally recommended that IF statements should not be nested more than three levels deep.
Key Benefits
- Reduced Complexity: By avoiding deep nesting of IF statements, the code is easier to read and understand.
- Increased Performance: Nested IF statements can be computationally expensive, so avoiding them can improve performance.
- Fewer Errors: By avoiding deep nesting, it is easier to spot errors in the code.
Non-compliant Code Example
BEGIN IF score > 35 THEN IF score > 40 THEN IF score > 50 THEN IF score > 60 THEN --Non compliant code (IF statements nested deeply beyond default defined 4 level) IF score > 70 THEN --Non compliant code (IF statements nested deeply beyond default defined 4 level) DBMS_OUTPUT.PUT('Grade A'); END IF; END IF; DBMS_OUTPUT.PUT('Grade B'); END IF; END IF; DBMS_OUTPUT.PUT('Grade C'); END IF; END;
Compliant Code Example
BEGIN
IF score > 35 AND score < 50 THEN
IF score > 50 AND score < 70 THEN
IF score > 70 THEN --Compliant code
DBMS_OUTPUT.PUT('Grade A');
END IF;
DBMS_OUTPUT.PUT('Grade B');
END IF;
DBMS_OUTPUT.PUT('Grade C');
END IF;
END;