Skip to content

G-4325: Never reuse labels in inner scopes.

Major

Maintainability, Reliability, Testability

Reason

Reusing labels inside the scope of another label with the same name leads to confusion, less chance of understanding the code, and could lead to bugs (for example if using exit my_label exits at a different nesting level than expected.)

Example (bad)

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

Example (good)

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