Skip to content

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)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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;
/