G-1050: Avoid using literals in your code.

Minor

Changeability

Reason

Literals are often used more than once in your code. Having them defined as a constant reduces typos in your code and improves the maintainability.

All constants should be collated in just one package used as a library. If these constants should be used in SQL too it is good practice to write a deterministic package function for every constant.

Example (bad)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
declare
   l_job employee.job_id%type;
begin
   select e.job_id
     into l_job
     from employee e
    where e.manager_id is null;

   if l_job = 'ad_pres' then
      null;
   end if;
exception
   when no_data_found then 
      null; -- handle_no_data_found;
   when too_many_rows then 
      null; -- handle_too_many_rows; 
end;
/

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
create or replace package constants is
   k_president constant employee.job_id%type := 'ad_pres';
end constants;
/

declare
   l_job employee.job_id%type;
begin
   select e.job_id
     into l_job
     from employee e
    where e.manager_id is null;

   if l_job = constants.k_president then
      null;
   end if;
exception
   when no_data_found then 
      null; -- handle_no_data_found;
   when too_many_rows then 
      null; -- handle_too_many_rows; 
end;
/