Avoid Reassigning Parameters
ID |
java.avoid_reassigning_parameters |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Java |
Tags |
reliability |
Description
Reports assignments whose left-hand side is a method parameter (including compound assignments like +=, -=, *=, …).
Rationale
Reassigning a parameter rebinds the local slot that holds the caller’s argument. After the reassignment the function no longer has access to the value the caller passed, which makes stack traces, log messages and debugger watches misleading ("I passed X, but the debugger says Y").
It is also a legibility problem: a parameter’s name is chosen to describe what the caller provided. Assigning a new value through the same name asks every subsequent reader to mentally track two distinct concepts on one identifier.
// Bad — 'input' now describes something different from what the caller passed
public String sanitize(String input) {
input = input.trim();
if (input == null) input = "";
return input;
}
Remediation
Introduce a local variable for the transformed value. This keeps the parameter available for debugging and communicates the transformation at the call site.
// Good — 'input' still holds the original argument; 'normalized' is the result
public String sanitize(String input) {
String normalized = input == null ? "" : input.trim();
return normalized;
}
Alternatively, declare the parameter final so the compiler rejects accidental reassignment.