rspec/rules/S2480/plsql/rule.adoc

65 lines
1.3 KiB
Plaintext
Raw Normal View History

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.
2021-04-28 16:49:39 +02:00
== Noncompliant Code Example
----
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;
/
----
2021-04-28 16:49:39 +02:00
== Compliant Solution
----
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)
include::message.adoc[]
include::parameters.adoc[]
'''
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]