Pattern: Reassigning parameter
Issue: -
Assigning new values to parameters is generally a bad practice unless an
operator such as ??=
is used. Otherwise, arbitrarily reassigning parameters
is usually a mistake.
Example of incorrect code:
void badFunction(int parameter) { // LINT
parameter = 4;
}
Example of incorrect code:
void badFunction(int required, {int optional: 42}) { // LINT
optional ??= 8;
}
Example of incorrect code:
void badFunctionPositional(int required, [int optional = 42]) { // LINT
optional ??= 8;
}
Example of incorrect code:
class A {
void badMethod(int parameter) { // LINT
parameter = 4;
}
}
Example of correct code:
void ok(String parameter) {
print(parameter);
}
Example of correct code:
void actuallyGood(int required, {int optional}) { // OK
optional ??= ...;
}
Example of correct code:
void actuallyGoodPositional(int required, [int optional]) { // OK
optional ??= ...;
}
Example of correct code:
class A {
void ok(String parameter) {
print(parameter);
}
}