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++)
; // ...
}
}
|