rspec/rules/S2480/plsql/rule.adoc

84 lines
1.5 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
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
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,sql]
2021-04-28 16:49:39 +02:00
----
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
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,sql]
2021-04-28 16:49:39 +02:00
----
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[]