Skip to content

G-7430: Try to use no more than one RETURN statement within a function.

Major

Maintainability, Testability

Reason

A function should have a single point of entry as well as a single exit-point.

Example (bad)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
create or replace package body my_package is
   function my_function(in_value in pls_integer) return boolean
      deterministic
   is
      co_yes constant pls_integer := 1;
   begin
      if in_value = co_yes then
         return true;
      else
         return false;
      end if;
   end my_function;
end my_package;
/

Example (better)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
create or replace package body my_package is
   function my_function(in_value in pls_integer) return boolean
      deterministic
   is
      co_yes constant pls_integer := 1;
      l_ret  boolean;
   begin
      if in_value = co_yes then
         l_ret := true;
      else
         l_ret := false;
      end if;

      return l_ret;
   end my_function;
end my_package;
/

Example (good)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
create or replace package body my_package is
   function my_function(in_value in pls_integer) return boolean
      deterministic
   is
      co_yes constant pls_integer := 1;
   begin
      return in_value = co_yes;
   end my_function;
end my_package;
/