Skip to content

Clarify text for unnecessary_final lint #58922

@timsneath

Description

@timsneath

The unnecessary_final lint contains a controversial explanation of its rationale.

Specifically:

var is shorter, and final does not change the meaning of the code.

The historical context is here: dart-archive/linter#1827

While there are clearly divergent preferences around the use of final, it does change the meaning of the code (as the prefer_final_locals lint explains). And while var is shorter by two characters, that seems like a poor rationale for a lint. Readability is preferred over terseness, anyway.

Lastly, Effective Dart has settled case law on the subject:

State that is not mutable—that does not change over time—is easier for programmers to reason about. Classes and libraries that minimize the amount of mutable state they work with tend to be easier to maintain. Of course, it is often useful to have mutable data. But, if you don’t need it, your default should be to make fields and top-level variables final when you can.

While we don't need to remove the rule, we should have explanatory text that is more accurate. For example, "some people prefer the simplicity of using var for all variables."

Metadata

Metadata

Assignees

Labels

P4devexp-linterIssues with the analyzer's support for the linter packagelegacy-area-analyzerUse area-devexp instead.type-documentationA request to add or improve documentation

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions