Skip to content

G-2160: Avoid initializing variables using functions in the declaration section.

Critical

Reliability

Reason

If your initialization fails, you will not be able to handle the error in your exceptions block.

Example (bad)

1
2
3
4
5
6
7
8
declare
   co_department_id  constant integer := 100;
   l_department_name departments.department_name%type :=
      department_api.name_by_id(in_id => co_department_id);
begin
   sys.dbms_output.put_line(l_department_name);
end;
/

Example (good)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
declare
   co_department_id  constant integer                          := 100;
   co_unkown_name    constant departments.department_name%type := 'unknown';
   l_department_name departments.department_name%type;
begin
   <<init>>
   begin
      l_department_name := department_api.name_by_id(in_id => co_department_id);
   exception
      when value_error then
         l_department_name := co_unkown_name;
   end init;

   sys.dbms_output.put_line(l_department_name);
end;
/