While it is technically correct to assign to parameters from within method bodies, it is typically done in error, with the intent to assign a
parameter value to a field of the same name, (and this was forgotten).
If it is done on purpose, a better course would be to use temporary variables to store intermediate results. Allowing parameters to be assigned to
also reduces code readability because developers won't be able to tell whether the original parameter or some temporary variable is being accessed
without going through the whole method. Moreover, some developers might also expect assignments of method parameters to be visible to callers, which
is not the case, and this lack of visibility could confuse them. Instead, all parameters, caught exceptions, and foreach parameters should be treated
as final.
function MyClass(name) {
name = name; // Noncompliant - useless identity assignment
}
function add(a, b) {
a = a + b; // Noncompliant
//...
return a;
}
function MyClass(name) {
this.name = name;
}
function add(a, b) {
let sum = a + b;
//...
return sum;
}