I'd argue that the first and foremost reason for not re-assigning parameter
values is because it's ambiguous and misleading, not because of side-effects.
If you've an operation that accepts something as a parameter and then needs to
modify that value, you probably haven't thought it through. Of course, there
are some obvious utility-type operations which are exceptions (something that
updates values in a Collection, frex).
Regards,
BRiaN
________________________________
From: Edward Sumerfield [mailto:esumerfd@xxxxxxxxx]
Sent: Wed 8/2/2006 10:50 AM
To: users@xxxxxxxxxx
Subject: [cinjug-users] IDE Warning - Parameter value should not be assigned
Is the warning "The parameter value should not be assigned" still valid in Java?
public void getFoo(int a_value, Object a_object)
{
a_value += 1; // WARNING
a_object = new Object(); // WARNING
}
It has always been good programming practice to avoid assigning parameters
because in C/C++ you might be setting a pointer that the caller would not know
had changed.
In java it is not possible set the object references that are passed in and the
primitive types are passed by value so what is the danger?
I propose that the old bad practive rule be revoked in the light of better
languages?
--
Ed
|