Home

Nested loops should be labeled

Description

    The rule "Nested loops should be labeled" states that when writing PL/SQL code, each loop should be labeled with a unique identifier. This helps to easily identify and debug the loop when necessary. Labeling nested loops also helps to improve the readability of the code, making it easier to understand and maintain.

Key Benefits

  • Labeling: Nested loops should be labeled to make the code easier to read and understand.
  • Organization: Labeling nested loops helps to organize the code and make it easier to debug.
  • Efficiency: Labeling nested loops can help to improve the efficiency of the code by making it easier to identify and optimize loops.

 

Non-compliant Code Example

BEGIN
  FOR order_table IN (SELECT * FROM orders) LOOP
    FOR order_items_table IN (SELECT * FROM order_items) LOOP       --Non compliant code (Nested loop is not labeled)
      DBMS_OUTPUT.PUT_LINE('Order Id: ' || order_table.id || ', Order Amount: ' || order_table.amount || ', Order Items: ' || order_items_table.Quantity);
    END LOOP;
  END LOOP;
END;

Compliant Code Example

BEGIN
	<<outer_for_loop>>
	FOR order_table IN (SELECT * FROM orders) LOOP
		<<inner_for_loop>> 
		FOR order_items_table IN (SELECT * FROM order_items) LOOP       --Compliant code (Nested loop is labeled)
			  DBMS_OUTPUT.PUT_LINE('Order Id: ' || order_table.id || ', Order Amount: ' || order_table.amount || ', Order Items: ' || order_items_table.Quantity);
		END LOOP inner_for_loop;
  END LOOP outer_for_loop;
END;
Visual Expert 2024
 VEPLSQLRULE157