Home

Collections should not be iterated in FOR loops

Rule description

  • Collections should not be iterated in FOR loops

 

Non-compliant Code Example

BEGIN
  FOR i IN 1 .. CUSTOMER_TABLE.COUNT --Non compliant code (Table data is iterated in the for loop)
  LOOP
		IF CUSTOMER_TABLE(i) IS NOT NULL THEN
			DBMS_OUTPUT.PUT( i || ' = (' || CUSTOMER_TABLE(i).Name || ', ' || CUSTOMER_TABLE(i).PhoneNumber || ')' );
		ELSE
			DBMS_OUTPUT.PUT( i || ' IS NULL' );
		END IF;
  END LOOP;
END;

Compliant Code Example

BEGIN

i := CUSTOMER_TABLE.FIRST;
  WHILE i IS NOT NULL LOOP --Compliant code (Table data is iterated in the while loop)
    IF CUSTOMER_TABLE(i) IS NOT NULL THEN
		DBMS_OUTPUT.PUT( i || ' = ('|| CUSTOMER_TABLE(i).Name || ', ' || CUSTOMER_TABLE(i).PhoneNumber || ')' );
    ELSE
		DBMS_OUTPUT.PUT( i || ' IS NULL' );
	END IF;
  END LOOP;
END
Visual Expert 2020
 VEPLSQLRULE31