G-4340: Always use a NUMERIC FOR loop to process a dense array.
Minor
Maintainability
Reason
It is easier for the reader to see, that the complete array is processed.
Since an EXIT
statement is similar to a GOTO
statement,
it should be avoided, whenever possible.
Example (bad)
DECLARE
TYPE t_employee_type IS VARRAY(10) OF employees.employee_id%TYPE;
t_employees t_employee_type;
co_himuro CONSTANT INTEGER := 118;
co_livingston CONSTANT INTEGER := 177;
co_min_value CONSTANT SIMPLE_INTEGER := 1;
co_increment CONSTANT SIMPLE_INTEGER := 1;
i PLS_INTEGER;
BEGIN
t_employees := t_employee_type(co_himuro, co_livingston);
i := co_min_value;
<<process_employees>>
LOOP
EXIT process_employees WHEN i > t_employees.COUNT();
sys.dbms_output.put_line(t_employees(i));
i := i + co_increment;
END LOOP process_employees;
END;
/
Example (good)
DECLARE
TYPE t_employee_type IS VARRAY(10) OF employees.employee_id%TYPE;
t_employees t_employee_type;
co_himuro CONSTANT INTEGER := 118;
co_livingston CONSTANT INTEGER := 177;
BEGIN
t_employees := t_employee_type(co_himuro, co_livingston);
<<process_employees>>
FOR i IN 1..t_employees.COUNT()
LOOP
sys.dbms_output.put_line(t_employees(i));
END LOOP process_employees;
END;
/