rspec/rules/S909/plsql/rule.adoc

68 lines
1.5 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2020-06-30 12:50:59 +02:00
FOR and WHILE loops are structured control flow statements.
2021-02-02 15:02:10 +01:00
2020-06-30 12:50:59 +02:00
A FOR loop will iterate once for each element in the range, and the WHILE iterates for as long as a condition holds.
2021-02-02 15:02:10 +01:00
2021-01-27 13:42:22 +01:00
However, inserting an ``++EXIT++`` statement within the loop breaks this structure, reducing the code's readability and making it harder to debug.
2020-06-30 12:50:59 +02:00
=== Noncompliant code example
2020-06-30 12:50:59 +02:00
2022-02-04 17:28:24 +01:00
[source,sql]
2020-06-30 12:50:59 +02:00
----
SET SERVEROUTPUT ON
DECLARE
TYPE myCollectionType IS VARRAY(10) OF VARCHAR2(42);
myCollection myCollectionType := myCollectionType('Foo', 'Bar', NULL, 'Baz', 'Qux');
i PLS_INTEGER;
BEGIN
i := 1;
WHILE i <= myCollection.LAST LOOP
EXIT WHEN myCollection(i) IS NULL; -- Noncompliant, breaks the structure of the WHILE
DBMS_OUTPUT.PUT_LINE('Got: ' || myCollection(i));
i := i + 1;
END LOOP;
----
=== Compliant solution
2020-06-30 12:50:59 +02:00
2022-02-04 17:28:24 +01:00
[source,sql]
2020-06-30 12:50:59 +02:00
----
SET SERVEROUTPUT ON
DECLARE
TYPE myCollectionType IS VARRAY(10) OF VARCHAR2(42);
myCollection myCollectionType := myCollectionType('Foo', 'Bar', NULL, 'Baz', 'Qux');
i PLS_INTEGER;
BEGIN
i := 1;
WHILE i <= myCollection.LAST AND myCollection(i) IS NOT NULL LOOP
DBMS_OUTPUT.PUT_LINE('Got: ' || myCollection(i));
i := i + 1;
END LOOP;
END;
/
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Remove this use of "EXIT".
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]