G-4390: Avoid use of unreferenced FOR loop indexes.
Major
Efficiency
Reason
If the loop index is used for anything but traffic control inside the loop, this is one of the indicators that a numeric FOR loop is being used incorrectly. The actual body of executable statements completely ignores the loop index. When that is the case, there is a good chance that you do not need the loop at all.
Example (bad)
DECLARE
l_row PLS_INTEGER;
l_value PLS_INTEGER;
co_lower_bound CONSTANT SIMPLE_INTEGER := 1;
co_upper_bound CONSTANT SIMPLE_INTEGER := 5;
co_row_incr CONSTANT SIMPLE_INTEGER := 1;
co_value_incr CONSTANT SIMPLE_INTEGER := 10;
co_delimiter CONSTANT types_up.short_text_type := ' ';
co_first_value CONSTANT SIMPLE_INTEGER := 100;
BEGIN
l_row := co_lower_bound;
l_value := co_first_value;
<<for_loop>>
FOR i IN co_lower_bound .. co_upper_bound
LOOP
sys.dbms_output.put_line(l_row || co_delimiter || l_value);
l_row := l_row + co_row_incr;
l_value := l_value + co_value_incr;
END LOOP for_loop;
END;
/
Example (good)
DECLARE
co_lower_bound CONSTANT SIMPLE_INTEGER := 1;
co_upper_bound CONSTANT SIMPLE_INTEGER := 5;
co_value_incr CONSTANT SIMPLE_INTEGER := 10;
co_delimiter CONSTANT types_up.short_text_type := ' ';
co_first_value CONSTANT SIMPLE_INTEGER := 100;
BEGIN
<<for_loop>>
FOR i IN co_lower_bound .. co_upper_bound
LOOP
sys.dbms_output.put_line(i || co_delimiter ||
to_char(co_first_value + i * co_value_incr));
END LOOP for_loop;
END;
/