-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Lint when local variables could be final
#57195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
+1 I use |
Not sure votes matter here, but in case they do: +1 :) |
'PREFER using |
The point made on another thread was that 'PREFER' is inherently flexible, which means that there can be good reasons to make another choice (like |
Right, I'm just saying that the way to make a different choice is to disable the rule for your project, not to create a separate conflicting rule.
One can't use
I think when one disagrees with a style guide rule, they have two options:
Separate rules for not type annotating and var vs. final would at least allow one to disable only the part the part they disagree with. There are probably a lot of style guide rules that could be split up for better configurability. |
On Wed, Mar 11, 2015 at 5:38 PM, Sean Eagan [email protected]
best regards, Erik Ernst - Google Danmark ApS |
How could you tell if a field is not used, if it is public? This would yield more false positives than real lint, for most code, I imagine. |
http://dart-lang.github.io/linter/lints/parameter_assignments.html taked care of the last part regarding parameters reassignments. |
The style guide says 'PREFER using var without a type annotation for local variables' (https://www.dartlang.org/articles/style-guide/#prefer-using-var-without-a-type-annotation-for-local-variables), which is sufficiently flexible to allow for an additional (per project or similar) rule along the lines of 'PREFER using final for local variables when this does not conflict with its usage'.
The rationale for this is that the explicit
final
annotation helps programmers thinking clearly about the distinction between variables that simply stand for a value (final
ones), and variables whose usage includes mutation (the remaining ones). For a mutated variable, the interplay between the control flow and the assignments to this variable must be taken into account when understanding what it does, or how to correctly modify the explicit occurrences of this variable, or how to correctly modify the control flow; hence, everyfinal
in the code simplifies comprehension.In order to help programmers maintain this discipline, it would be useful to have a lint diagnostic on cases where a variable is never mutated, but still it is not marked as
final
.A similar argument could be made for formal parameters in functions/methods, but presumably there is a culture around the treatment of such formals which is so strict that it is acceptable to outlaw mutation at all (and a lint diagnostic is given for all mutations, as proposed in #57194).
The text was updated successfully, but these errors were encountered: