G-1020: Always have a matching loop or block label.
Minor
Maintainability
Reason
Use a label directly in front of loops and nested anonymous blocks:
- To give a name to that portion of code and thereby self-document what it is doing.
- So that you can repeat that name with the END statement of that block or loop.
Example (bad)
DECLARE
i INTEGER;
co_min_value CONSTANT INTEGER := 1;
co_max_value CONSTANT INTEGER := 10;
co_increment CONSTANT INTEGER := 1;
BEGIN
<<prepare_data>>
BEGIN
NULL;
END;
<<process_data>>
BEGIN
NULL;
END;
i := co_min_value;
<<while_loop>>
WHILE (i <= co_max_value)
LOOP
i := i + co_increment;
END LOOP;
<<basic_loop>>
LOOP
EXIT basic_loop;
END LOOP;
<<for_loop>>
FOR i IN co_min_value..co_max_value
LOOP
sys.dbms_output.put_line(i);
END LOOP;
END;
/
Example (good)
DECLARE
i INTEGER;
co_min_value CONSTANT INTEGER := 1;
co_max_value CONSTANT INTEGER := 10;
co_increment CONSTANT INTEGER := 1;
BEGIN
<<prepare_data>>
BEGIN
NULL;
END prepare_data;
<<process_data>>
BEGIN
NULL;
END process_data;
i := co_min_value;
<<while_loop>>
WHILE (i <= co_max_value)
LOOP
i := i + co_increment;
END LOOP while_loop;
<<basic_loop>>
LOOP
EXIT basic_loop;
END LOOP basic_loop;
<<for_loop>>
FOR i IN co_min_value..co_max_value
LOOP
sys.dbms_output.put_line(i);
END LOOP for_loop;
END;
/