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)

CREATE OR REPLACE PACKAGE BODY my_package IS
   PROCEDURE my_procedure IS
      l_idx SIMPLE_INTEGER := 1;
      co_modulo CONSTANT SIMPLE_INTEGER := 7;
   BEGIN
      <<mod7_loop>>
      LOOP
        IF MOD(l_idx,co_modulo) = 0 THEN
           RETURN;
        END IF;

        l_idx := l_idx + 1;
      END LOOP mod7_loop;
   END my_procedure;
END my_package;
/

Example (good)

CREATE OR REPLACE PACKAGE BODY my_package IS
   PROCEDURE my_procedure IS
      l_idx SIMPLE_INTEGER := 1;
      co_modulo CONSTANT SIMPLE_INTEGER := 7;
   BEGIN
      <<mod7_loop>>
      LOOP
        EXIT mod7_loop WHEN MOD(l_idx,co_modulo) = 0;

        l_idx := l_idx + 1;
      END LOOP mod7_loop;
   END my_procedure;
END my_package;
/