G-7320
🆓Warning
Avoid using RETURN statements in a PROCEDURE.
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
Non-Compliant Example
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;
/Issues
| Line | Column | Message |
|---|---|---|
| 9 | 13 |
Compliant Solution - ★★★★★
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;
/References
- similar to plsql:S3626
The scope of plsql:S3626 is RETURN and CONTINUE statements in general, not just in procedures.
- same as plsql:ReturnInProcedureCheck
- same as Trivadis G-7320