G-7320: Avoid using RETURN statements in a PROCEDURE.

Major

Maintainability, Testability

Reason

Use of the return statement is legal within a procedure in PL/SQL, but it is very similar to a goto, which means you end up with poorly structured code that is hard to debug and maintain.

A good general rule to follow as you write your PL/SQL programs is "one way in and one way out". In other words, there should be just one way to enter or call a program, and there should be one way out, one exit path from a program (or loop) on successful termination. By following this rule, you end up with code that is much easier to trace, debug, and maintain.

Example (bad)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
create or replace package body my_package is
   procedure my_procedure is
      l_idx simple_integer := 1;
      k_modulo constant simple_integer := 7;
   begin
      <<mod7_loop>>
      loop
        if mod(l_idx,k_modulo) = 0 then
           return;
        end if;

        l_idx := l_idx + 1;
      end loop mod7_loop;
   end my_procedure;
end my_package;
/

Example (good)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
create or replace package body my_package is
   procedure my_procedure is
      l_idx simple_integer := 1;
      k_modulo constant simple_integer := 7;
   begin
      <<mod7_loop>>
      loop
        exit mod7_loop when mod(l_idx,k_modulo) = 0;

        l_idx := l_idx + 1;
      end loop mod7_loop;
   end my_procedure;
end my_package;
/