Skip to content

G-4380: Try to label your EXIT WHEN statements.

Minor

Maintainability

Reason

It's a good alternative for comments, especially for nested loops to name the loop to exit.

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
declare
   co_init_loop  constant simple_integer           := 0;
   co_increment  constant simple_integer           := 1;
   co_exit_value constant simple_integer           := 3;
   co_outer_text constant types_up.short_text_type := 'Outer Loop counter is ';
   co_inner_text constant types_up.short_text_type := ' Inner Loop counter is ';
   l_outerlp     pls_integer;
   l_innerlp     pls_integer;
begin
   l_outerlp := co_init_loop;
   <<outerloop>>
   loop
      l_innerlp := co_init_loop;
      l_outerlp := nvl(l_outerlp,co_init_loop) + co_increment;
      <<innerloop>>
      loop
         l_innerlp := nvl(l_innerlp,co_init_loop) + co_increment;
         sys.dbms_output.put_line(co_outer_text
            || l_outerlp
            || co_inner_text
            || l_innerlp);

         exit when l_innerlp = co_exit_value;
      end loop innerloop;

      exit when l_innerlp = co_exit_value;
   end loop outerloop;
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
declare
   co_init_loop  constant simple_integer           := 0;
   co_increment  constant simple_integer           := 1;
   co_exit_value constant simple_integer           := 3;
   co_outer_text constant types_up.short_text_type := 'Outer Loop counter is ';
   co_inner_text constant types_up.short_text_type := ' Inner Loop counter is ';
   l_outerlp     pls_integer;
   l_innerlp     pls_integer;
begin
   l_outerlp := co_init_loop;
   <<outerloop>>
   loop
      l_innerlp := co_init_loop;
      l_outerlp := nvl(l_outerlp,co_init_loop) + co_increment;
      <<innerloop>>
      loop
         l_innerlp := nvl(l_innerlp,co_init_loop) + co_increment;
         sys.dbms_output.put_line(co_outer_text
            || l_outerlp
            || co_inner_text
            || l_innerlp);

         exit outerloop when l_innerlp = co_exit_value;
      end loop innerloop;
   end loop outerloop;
end;
/