rules repository

G-1050

🆓
Warning

Avoid using literals in your code.

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.

Literals are only considered in stored objects. Furthermore literals in exception handlers, JSON basic path expressions, JSON regular entries and json_transform function are ignored.

Example

Non-Compliant Example

create or replace procedure p is
begin
   some_api.setup(in_department_id => 10);
   some_api.process(in_department_id => 10);
   some_api.teardown(in_department_id => 10);
end;
/
Issues
LineColumnMessage
339Literal 10 used more than 1 time.
441Literal 10 used more than 1 time.
542Literal 10 used more than 1 time.

Compliant Solution - ★★★★★

create or replace package constants_up is
   co_dept_admin constant departments.department_id%type := 10;
end constants_up;
/

create or replace procedure p is
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;
/

Parameters

Use parameters to customize the rule to your needs.

ParameterDescriptionDefault Value
LoggingProcedureCallsComma-separated list of packages and procedures for which literals are ignored in procedure calls.dbms_output, dbl_log_api
MaxNumberOfLiteralsMaximum number of same literal allowed in a file. An issue is reported when the value is exceeded.1
ConversionFunctionsComma-separated list of conversion functions for which required literals should be ignored (e.g. within nlsparams).ceil, floor, nls_initcap, nls_lower, nls_upper, nlssort, numtodsinterval, numtoyminterval, round, sys_xmlagg, sys_xmlgen, to_binary_double, to_binary_float, to_char, to_date, to_nchar, to_number, to_timestamp, to_timestamp_tz, trunc, validate_conversion, xmlsequence
AllowedMagicValuesComma-separated List of allowed literals in code.-1, 0, 1

References