Home

Nested blocks should be labeled

Description

    The "Nested blocks should be labeled" rule states that when writing PL/SQL code, all nested blocks should be labeled with a unique identifier. This helps to make the code more readable and easier to debug. Labels should be descriptive and should include the type of block (e.g. IF, LOOP, etc.) as well as the purpose of the block. Labels should be placed at the beginning of the block and should be followed by a colon. This rule helps to ensure that the code is easier to understand and maintain.

Key Benefits

  • Easier to read code: Nested blocks should be labeled rule makes code easier to read by providing a clear structure.
  • Reduces complexity: Labeling nested blocks reduces complexity and makes it easier to debug and maintain code.
  • Improves code quality: Labeling nested blocks improves code quality by making it easier to understand and follow.
  • Helps with debugging: Labeling nested blocks helps with debugging by providing a clear structure to follow.

 

Non-compliant Code Example

DECLARE
  first_name  VARCHAR2(25);
  customer_id     NUMBER(6) := 120;
BEGIN
  	
  <<get_customer_name>>
  SELECT FIRSTNAME INTO first_name
  FROM CUSTOMERS
  WHERE Id = customer_id;
  
  BEGIN                 --Non compliant code (Nested block is not labeled)
    DBMS_OUTPUT.PUT_LINE (first_name);
    customer_id := customer_id + 1;
 
    IF customer_id < 200 THEN
      GOTO get_customer_name;
    END IF;
  
  END;
END;

Compliant Code Example

DECLARE
  first_name  VARCHAR2(25);
  customer_id     NUMBER(6) := 120;
BEGIN
  	
  <<get_customer_name>>
  SELECT FIRSTNAME INTO first_name
  FROM CUSTOMERS
  WHERE Id = customer_id;
  
    <<get_customer_name_inner>>
    BEGIN               --Compliant code (Nested block is labeled)
        DBMS_OUTPUT.PUT_LINE (first_name);
        customer_id := customer_id + 1;
 
        IF customer_id < 200 THEN
          GOTO get_customer_name;
        END IF;
    END;
END;
Visual Expert 2024
 VEPLSQLRULE158