Skip to content

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.

To avoid an extreme plethora of constants or false positives, a literal should not occur more than once within a file.

Example (bad)

1
2
3
4
5
6
begin
   some_api.setup(in_department_id => 10);
   some_api.process(in_department_id => 10);
   some_api.teardown(in_department_id => 10);
end;
/

Example (good)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
create or replace package constants_up is
   co_dept_admin constant departments.department_id%type := 10;
end constants_up;
/

begin
   some_api.setup(in_department_id => constants_up.co_dept_admin);
   some_api.process(in_department_id => constants_up.co_dept_admin);
   some_api.teardown(in_department_id => constants_up.co_dept_admin);
end;
/