G-4395
🆓Warning
Avoid hard-coded upper or lower bound values with FOR loops.
Reason
Your loop statement uses a hard-coded value for either its upper or lower bounds. This creates a "weak link" in your program because it assumes that this value will never change. A better practice is to create a named constant (or function) and reference this named element instead of the hard-coded value.
Example
Non-Compliant Example
begin
<<for_loop>>
for i in 2..5
loop
sys.dbms_output.put_line(i);
end loop for_loop;
end;
/Issues
| Line | Column | Message |
|---|---|---|
| 3 | 13 | |
| 3 | 16 |
Compliant Solution - ★★★★★
declare
co_lower_bound constant simple_integer := 2;
co_upper_bound constant simple_integer := 5;
begin
<<for_loop>>
for i in co_lower_bound..co_upper_bound
loop
sys.dbms_output.put_line(i);
end loop for_loop;
end;
/Parameters
Use parameters to customize the rule to your needs.
| Parameter | Description | Default Value |
|---|---|---|
| AllowedMagicValues | Comma-separated List of allowed literals in code. | -1, 0, 1 |
References
- same as Trivadis G-4395
- same as plsql:LoopHardcodedBoundsCheck