Skip to content

G-2135: Avoid assigning values to local variables that are not used by a subsequent statement.

Major

Efficiency, Maintainability, Testability

Reason

Expending resources calculating and assigning values to a local variable and never use the value subsequently is at best a waste, at worst indicative of a mistake that leads to a bug.

Example (bad)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
create or replace package body my_package is
   procedure my_proc is
      co_employee_id constant employees.employee_id%type := 1042;
      co_hello       constant type_up.text               := 'Hello, ';
      l_last_name    employees.last_name%type;
      l_message      types_up.text;
   begin
      select emp.last_name
        into l_last_name
        from employees emp
       where emp.employee_id = co_employee_id;

      l_message := co_hello || l_last_name;
   exception
      when no_data_found then
         null; -- handle_no_data_found;
      when too_many_rows then
         null; -- handle_too_many_rows;
   end my_proc;
end my_package;
/

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 body my_package is
   procedure my_proc is
      co_employee_id constant employees.employee_id%type := 1042;
      co_hello       constant type_up.text               := 'Hello, ';
      l_last_name    employees.last_name%type;
      l_message      types_up.text;
   begin
      select emp.last_name
        into l_last_name
        from employees emp
       where emp.employee_id = co_employee_id;

      l_message := co_hello || l_last_name;

      message_api.send_message(l_message);
   exception
      when no_data_found then
         null; -- handle_no_data_found;
      when too_many_rows then
         null; -- handle_too_many_rows;
   end my_proc;
end my_package;
/