55 lines
1.2 KiB
Plaintext
55 lines
1.2 KiB
Plaintext
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
|
|
|
|
----
|
|
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
|
|
|
|
----
|
|
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::rspecator-view[]
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::comments-and-links.adoc[]
|
|
endif::rspecator-view[]
|