Skip to content

G-7220: Always use forward declaration for private functions and procedures.

Minor

Changeability

Reason

Having forward declarations allows you to order the functions and procedures of the package in a reasonable way.

Example (bad)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
create or replace package department_api is
   procedure del(in_department_id in departments.department_id%type);
end department_api;
/

create or replace package body department_api is
   function does_exist(in_department_id in departments.department_id%type)
      return boolean is
      l_return pls_integer;
   begin
      <<check_row_exists>>
      begin
         select 1
           into l_return
           from departments
          where department_id = in_department_id;
      exception
         when no_data_found or too_many_rows then
            l_return := 0;
      end check_row_exists;

      return l_return = 1;
   end does_exist;

   procedure del(in_department_id in departments.department_id%type) is
   begin
      if does_exist(in_department_id) then
         null;
      end if;
   end del;
end department_api;
/

Example (good)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
create or replace package department_api is
   procedure del(in_department_id in departments.department_id%type);
end department_api;
/

create or replace package body department_api is
   function does_exist(in_department_id in departments.department_id%type)
      return boolean;

   procedure del(in_department_id in departments.department_id%type) is
   begin
      if does_exist(in_department_id) then
         null;
      end if;
   end del;

   function does_exist(in_department_id in departments.department_id%type)
      return boolean is
      l_return pls_integer;
   begin
      <<check_row_exists>>
      begin
         select 1
           into l_return
           from departments
          where department_id = in_department_id;
      exception
         when no_data_found or too_many_rows then
            l_return := 0;
      end check_row_exists;

      return l_return = 1;
   end does_exist;
end department_api;
/