
Inline adoc files when they are included exactly once. Also fix language tags because this inlining gives us better information on what language the code is written in.
84 lines
1.5 KiB
Plaintext
84 lines
1.5 KiB
Plaintext
== Why is this an issue?
|
|
|
|
Hard-coding bounds in FOR loops is a bad practice, just as magic numbers in general are. Often, those magic bounds can be replaced by dynamic values. If that is not possible, replacing the literal number with a constant is still better.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,sql]
|
|
----
|
|
SET SERVEROUTPUT ON
|
|
|
|
DECLARE
|
|
TYPE myCollectionType IS VARRAY(3) OF VARCHAR2(42);
|
|
myCollection myCollectionType := myCollectionType('David', 'John', 'Richard');
|
|
|
|
BEGIN
|
|
|
|
FOR i IN 2 .. 3 -- Noncompliant; magic numbers used for the loop bounds
|
|
LOOP
|
|
DBMS_OUTPUT.PUT_LINE('name = ' || myCollection(i));
|
|
END LOOP;
|
|
|
|
FOR i IN 2 .. myCollection.LAST -- Noncompliant, better but still magic
|
|
LOOP
|
|
DBMS_OUTPUT.PUT_LINE('name = ' || myCollection(i));
|
|
END LOOP;
|
|
|
|
END;
|
|
/
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,sql]
|
|
----
|
|
SET SERVEROUTPUT ON
|
|
|
|
DECLARE
|
|
TYPE myCollectionType IS VARRAY(3) OF VARCHAR2(42);
|
|
myCollection myCollectionType := myCollectionType('David', 'John', 'Richard');
|
|
|
|
BEGIN
|
|
FOR i IN myCollection.FIRST .. myCollection.LAST
|
|
LOOP
|
|
DBMS_OUTPUT.PUT_LINE('name = ' || myCollection(i));
|
|
END LOOP;
|
|
END;
|
|
/
|
|
----
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Use a constant instead of hard-coding "n".
|
|
|
|
|
|
=== Parameters
|
|
|
|
.allowedExpressions
|
|
****
|
|
|
|
----
|
|
0,1
|
|
----
|
|
|
|
Comma-separated list of allowed magic numbers
|
|
****
|
|
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
=== relates to: S109
|
|
|
|
endif::env-github,rspecator-view[]
|