Background
Flutter's code style has typically not enforced whether or not parameters can be modified in function bodies.
void greet(String name) {
name = 'The Respectable $name'; // Is this ok? Styleguide and linter don't care.
return 'Hello $name';
}
It has been possible to use final to enforce that a parameter can't be modified:
void greet(final String name) {
name = 'The Respectable $name'; // Compilation error.
...
But recently, PR #185216 was landed that disallowed using final in this way (for good reasons, see the PR for details).
Proposal
I propose that Flutter should adopt the parameter_assignments lint, which would enforce that parameters cannot be modified.
void greet(String name) {
// name = 'The Respectable $name'; // Lint error.
final String nameWithTitle = 'The Respectable $name';
return 'Hello $nameWithTitle';
}
In my opinion, having the ability to modify parameters increases the mental burden of understanding a function (e.g., "I want to use this parameter name, but has it been changed? I have to read through all previous lines of code in the function to be sure that it hasn't.").
This will have to be done in pieces due to the code freeze.
Background
Flutter's code style has typically not enforced whether or not parameters can be modified in function bodies.
It has been possible to use
finalto enforce that a parameter can't be modified:But recently, PR #185216 was landed that disallowed using
finalin this way (for good reasons, see the PR for details).Proposal
I propose that Flutter should adopt the parameter_assignments lint, which would enforce that parameters cannot be modified.
In my opinion, having the ability to modify parameters increases the mental burden of understanding a function (e.g., "I want to use this parameter name, but has it been changed? I have to read through all previous lines of code in the function to be sure that it hasn't.").
This will have to be done in pieces due to the code freeze.