rules repository

G-4375

🆓
Warning

Always use EXIT WHEN instead of an IF statement to exit from a loop.

Reason

If you need to use an exit statement use its full semantic to make the code easier to understand and maintain. There is simply no need for an additional if statement.

Example

Non-Compliant Example

declare
   co_first_year constant pls_integer := 1900;
begin
   <<process_employees>>
   loop
      my_package.some_processing();

      if extract(year from sysdate) > co_first_year then
         exit process_employees;
      end if;

      my_package.some_further_processing();
   end loop process_employees;
end;
/
Issues
LineColumnMessage
87Always use EXIT WHEN instead of an IF statement to exit from a loop.

Compliant Solution - ★★★★★

declare
   co_first_year constant pls_integer := 1900;
begin
   <<process_employees>>
   loop
      my_package.some_processing();

      exit process_employees when extract(year from sysdate) > co_first_year;

      my_package.some_further_processing();
   end loop process_employees;
end;
/

References