G-7250: Never use RETURN in package initialization block.
Reason
The purpose of the initialization block of a package body is to set initial values of the global variables of the package (initialize the package state). Although return
is syntactically allowed in this block, it makes no sense. If it is the last keyword of the block, it is superfluous. If it is not the last keyword, then all code after the return
is unreachable and thus dead code.
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 | create or replace package body employee_api as
g_salary_increase types_up.sal_increase_type;
procedure set_salary_increase(in_increase in types_up.sal_increase_type) is
co_increase constant types_up.sal_increase_type := in_increase;
begin
g_salary_increase := greatest(least(co_increase,constants_up.max_salary_increase())
,constants_up.min_salary_increase());
end set_salary_increase;
function salary_increase return types_up.sal_increase_type is -- NOSONAR: non-deterministic
begin
return g_salary_increase;
end salary_increase;
begin
g_salary_increase := constants_up.min_salary_increase();
return; -- violates also G-1040
set_salary_increase(constants_up.min_salary_increase()); -- dead code
end employee_api;
/
|
Example (good)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 | create or replace package body employee_api as
g_salary_increase types_up.sal_increase_type;
procedure set_salary_increase(in_increase in types_up.sal_increase_type) is
co_increase constant types_up.sal_increase_type := in_increase;
begin
g_salary_increase := greatest(least(co_increase,constants_up.max_salary_increase())
,constants_up.min_salary_increase());
end set_salary_increase;
function salary_increase return types_up.sal_increase_type is -- NOSONAR: non-deterministic
begin
return g_salary_increase;
end salary_increase;
begin
g_salary_increase := constants_up.min_salary_increase();
end employee_api;
/
|