G-7220: Always use forward declaration for private functions and procedures.
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
33
34 | 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) -- violates also G-7460
return boolean is
co_department_id constant departments.department_id%type := in_department_id;
l_return pls_integer;
begin
<<check_row_exists>>
begin
select 1
into l_return
from departments
where department_id = co_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
co_department_id constant departments.department_id%type := in_department_id;
begin
if does_exist(co_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
36
37 | 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) -- NOSONAR: non-deterministic
return boolean;
procedure del(in_department_id in departments.department_id%type) is
co_department_id constant departments.department_id%type := in_department_id;
begin
if does_exist(co_department_id) then
null;
end if;
end del;
function does_exist(in_department_id in departments.department_id%type) -- NOSONAR: non-deterministic
return boolean is
co_department_id constant departments.department_id%type := in_department_id;
l_return pls_integer;
begin
<<check_row_exists>>
begin
select 1
into l_return
from departments
where department_id = co_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;
/
|