Home

Loops with at most one iteration should be refactored

Rule description

  • Loops with at most one iteration should be refactored

 

Non-compliant Code Example

DECLARE 
   id customers.id%type; 
   name customer.name%type; 
   address customers.address%type; 
   CURSOR customers_c is 
      SELECT id, name, address FROM customers; 
BEGIN 
   OPEN customers_c; 
   <<INNER_LOOP>>
   LOOP                 --Non compliant code (Loops with one iteration)
   FETCH customers_c into id, name, address; 
      EXIT WHEN customers_c%notfound; 
      dbms_output.put_line(id || ' ' || name || ' ' || address); 
   END LOOP INNER_LOOP; 
   CLOSE customers_c; 
END; 

Compliant Code Example

DECLARE 
   id customers.id%type; 
   name customer.name%type; 
   address customers.address%type; 
   CURSOR customers_c is 
      SELECT id, name, address FROM customers; 
BEGIN 
   OPEN customers_c;  --Compliant code (No need of loop for single iteration)
   FETCH customers_c into id, name, address; 
      EXIT WHEN customers_c%notfound; 
      dbms_output.put_line(id || ' ' || name || ' ' || address); 
   CLOSE customers_c; 
END; 
Visual Expert 2020
 VEPLSQLRULE62