Skip to content

Commit e10f537

Browse files
committed
Fix #48 - Created rule G-2135: Avoid assigning values to local variables that are not used by a subsequent statement.
1 parent 5af501b commit e10f537

File tree

2 files changed

+57
-1
lines changed
  • docs/4-language-usage

2 files changed

+57
-1
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# G-2135: Avoid assigning values to local variables that are not used by a subsequent statement.
2+
3+
!!! warning "Major"
4+
Efficiency, Maintainability, Testability
5+
6+
## Reason
7+
8+
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.
9+
10+
## Example (bad)
11+
12+
``` sql
13+
create or replace package body my_package is
14+
procedure my_proc is
15+
co_employee_id constant employees.employee_id%type := 1042;
16+
l_last_name employees.last_name%type;
17+
l_message varchar2(100 char);
18+
begin
19+
select emp.last_name
20+
into l_last_name
21+
from employees emp
22+
where emp.employee_id = co_employee_id;
23+
24+
l_message := 'Hello, ' || l_last_name;
25+
exception
26+
when no_data_found then null; -- handle_no_data_found;
27+
when too_many_rows then null; -- handle_too_many_rows;
28+
end my_proc;
29+
end my_package;
30+
/
31+
```
32+
33+
## Example (good)
34+
35+
``` sql
36+
create or replace package body my_package is
37+
procedure my_proc is
38+
co_employee_id constant employees.employee_id%type := 1042;
39+
l_last_name employees.last_name%type;
40+
l_message varchar2(100 char);
41+
begin
42+
select emp.last_name
43+
into l_last_name
44+
from employees emp
45+
where emp.employee_id = co_employee_id;
46+
47+
l_message := 'Hello, ' || l_last_name;
48+
49+
message_api.send_message(l_message);
50+
exception
51+
when no_data_found then null; -- handle_no_data_found;
52+
when too_many_rows then null; -- handle_too_many_rows;
53+
end my_proc;
54+
end my_package;
55+
/
56+
```

docs/4-language-usage/7-stored-objects/3-procedures/g-7330.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ create or replace package body my_package is
1515
in_name in varchar2
1616
, out_greeting out varchar2
1717
) is
18-
l_message varchar2(100);
18+
l_message varchar2(100 char);
1919
begin
2020
l_message := 'Hello, ' || in_name;
2121
end my_procedure;

0 commit comments

Comments
 (0)