Home

Constant declarations should contain initialization assignments

Description

    The rule "Constant declarations should contain initialization assignments" states that when declaring a constant in PL/SQL, it should be initialized with an assignment. This means that when a constant is declared, its value should be set immediately. This ensures that the constant is always initialized with a value and can be used in the code without any unexpected behavior.

Key Benefits

  • Eliminates ambiguity - Constant declarations should contain initialization assignments to eliminate any ambiguity about the value of the constant.
  • Reduces errors - By explicitly assigning a value to a constant, it reduces the chances of errors caused by typos or incorrect values.
  • Improves readability - Constants initialization assignments are easier to read and understand, which improves the readability of the code.

 

Non-compliant Code Example

DECLARE
  pi     CONSTANT REAL;     --Non compliant code (Constant declaration does not contains initialization assignments)
  radius          REAL := 1;
  area            REAL := (pi * radius**2);
BEGIN
  NULL;
END;

Compliant Code Example

DECLARE
  pi     CONSTANT REAL := 3.14159;  --Compliant code (Constant declaration contains initialization assignments)
  radius          REAL := 1;
  area            REAL := (pi * radius**2);
BEGIN
  NULL;
END;
Visual Expert 2024
 VEPLSQLRULE13