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
   k_department_id constant integer := 100;
   l_department_name department.department_name%type := 
      department_api.name_by_id(in_id => k_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
   k_department_id  constant integer := 100;
   k_unkown_name    constant department.department_name%type := 'unknown';
   l_department_name department.department_name%type;
begin
   <<init>>
   begin
      l_department_name := department_api.name_by_id(in_id => k_department_id);
   exception
      when value_error then
         l_department_name := k_unkown_name;
   end init;

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