Jtest logo




Contents  Previous  Next  Index

OPT.CEL


Avoid using complex expressions in loop condition statements

Description

This rule flags any complex expression in a loop condition statement.

Unless the compiler optimizes it, the loop condition will be calculated for each iteration over the loop. If the condition value is not going to change, moving the complex expression out of the loop will cause the code to execute faster.

Example

 package OPT;
 import java.util.Vector;
 class CEL
 {
     void method (Vector vector) {
         for (int i = 0; i < vector.size (); i++)  // Violation
             ; // ...
     }
 }

Repair

 class CEL_fixed
 {
     void method (Vector vector) {
         int size = vector.size ()
         for (int i = 0; i < size; i++)
             ; // ...
     }
 }

Contents  Previous  Next  Index

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