Jtest logo




Contents  Previous  Next  Index

OPT.LOOP


Avoid instantiating variables in the loop body

Description

This rule flags any variable that is instantiated in the loop body.

Instantiating temporary Objects in the loop body can increase the memory management overhead.

Example

 package OPT;
 import java.util.Vector;
 
 public class LOOP {
     void method (Vector v) {
         for (int i=0;i < v.isEmpty();i++) {
             Object o = new Object();
             o = v.elementAt(i);
         }
     }
 }

Repair

Declare a variable outside of the loop and reuse this variable.

 package OPT;
 import java.util.Vector;
 
 public class LOOP {
     void method (Vector v) {
         Object o;
         for (int i=0;i<v.isEmpty();i++) {
             o = v.elementAt(i);
         }
     }
 }

Contents  Previous  Next  Index

ParaSoft logo
(888) 305-0041 info@parasoft.com Copyright © 1996-2001 ParaSoft