rules repository

G-4320

🆓
Warning

Always label your loops.

Reason

It's a good alternative for comments to indicate the start and end of a named loop processing.

Example

Non-Compliant Example

declare
   i            integer;
   co_min_value constant simple_integer := 1;
   co_max_value constant simple_integer := 10;
   co_increment constant simple_integer := 1;
begin
   i := co_min_value;
   while (i <= co_max_value)
   loop
      i := i + co_increment;
   end loop;

   loop
      exit when true;
   end loop;

   for i in co_min_value..co_max_value
   loop
      sys.dbms_output.put_line(i);
   end loop;

   for r_employee in (select last_name from employees)
   loop
      sys.dbms_output.put_line(r_employee.last_name);
   end loop;
end;
/
Issues
LineColumnMessage
84Missing start label in while loop statement.
118Missing end label in while loop statement.
134Missing start label in basic loop statement.
158Missing end label in basic loop statement.
174Missing start label in for loop statement.
208Missing end label in for loop statement.
224Missing start label in cursor for loop statement.
258Missing end label of cursor for loop statement.

Compliant Solution - ★★★★★

declare
   i            integer;
   co_min_value constant simple_integer := 1;
   co_max_value constant simple_integer := 10;
   co_increment constant simple_integer := 1;
begin
   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 when true;
   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;

   <<process_employees>>
   for r_employee in (select last_name from employees)
   loop
      sys.dbms_output.put_line(r_employee.last_name);
   end loop process_employees;
end;
/

References