Skip to content

G-4395: Avoid hard-coded upper or lower bound values with FOR loops.

Minor

Changeability, Maintainability

Reason

Your loop statement uses a hard-coded value for either its upper or lower bounds. This creates a "weak link" in your program because it assumes that this value will never change. A better practice is to create a named constant (or function) and reference this named element instead of the hard-coded value.

Example (bad)

1
2
3
4
5
6
7
8
begin
   <<for_loop>>
   for i in 1..5
   loop
      sys.dbms_output.put_line(i);
   end loop for_loop;
end;
/

Example (good)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
declare
   co_lower_bound constant simple_integer := 1;
   co_upper_bound constant simple_integer := 5;
begin
   <<for_loop>>
   for i in co_lower_bound..co_upper_bound
   loop
      sys.dbms_output.put_line(i);
   end loop for_loop;
end;
/