Jtest logo




Contents  Previous  Next  Index

MISC.AFP


Avoid assignment to method parameters

Description

This rule flags any assignment to method parameters.

Assignment operations on a method parameter can cause problems when the value of the parameter is used more than once in a block.

Example

 package MISC;
 
 class AFP {
     int avg (int x) {
         int count = 0;
         while (x++ < 10) {
         count += x;
         }
     return count % x; // x no longer has the same value.
     }
 }

Repair

 //create a local variable.
 class AFP {
     int avg (int x) {
         int count = 0;
         int i = x;
         while (i++ < 10) {
             count += i;
         }
         return count % i;
     }
 }

Contents  Previous  Next  Index

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