Avoid to update parameters inside routine’s bodies

Why you should care

Reassigning parameter of function to a new value within the body of the method/closure, is a confusing and questionable practice. Use a temporary variable instead.

How we detect

CAST Highlight counts one occurrence each time a parameter is updated inside the body of the function/method/closure, using the following operators =, +=, -=, *=, /=, %=, ++, – -.

class toto {
String field = "";
public String getFoo(int param, String field)
{
param = 0 // +1 VIOLATION
param++ // +1 VIOLATION
param -= 10 // +1 VIOLATION

a = (param == 0 ? 1 : null) // OK
my_param = 0 //OK

this.field = "Yo!" // OK because field is not the parameter.
}
}

References

5362

About CAST and Highlight’s Code Insights

Over the last 25 years, CAST has leveraged unique knowledge on software quality measurement by analyzing thousands of applications and billions of lines of code. Based on this experience and community standards on programming best practices, Highlight implements hundreds of code insights across 15+ technologies to calculate health factors of a software.