{"action":"created","issue":{"url":"https://api.github.com/repos/dart-lang/linter/issues/46","labels_url":"https://api.github.com/repos/dart-lang/linter/issues/46/labels{/name}","comments_url":"https://api.github.com/repos/dart-lang/linter/issues/46/comments","events_url":"https://api.github.com/repos/dart-lang/linter/issues/46/events","html_url":"https://github.com/dart-lang/linter/issues/46","id":60632798,"number":46,"title":"Lint when local variables could be `final`","user":{"login":"eernstg","id":9606994,"avatar_url":"https://avatars.githubusercontent.com/u/9606994?v=3","gravatar_id":"","url":"https://api.github.com/users/eernstg","html_url":"https://github.com/eernstg","followers_url":"https://api.github.com/users/eernstg/followers","following_url":"https://api.github.com/users/eernstg/following{/other_user}","gists_url":"https://api.github.com/users/eernstg/gists{/gist_id}","starred_url":"https://api.github.com/users/eernstg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eernstg/subscriptions","organizations_url":"https://api.github.com/users/eernstg/orgs","repos_url":"https://api.github.com/users/eernstg/repos","events_url":"https://api.github.com/users/eernstg/events{/privacy}","received_events_url":"https://api.github.com/users/eernstg/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/dart-lang/linter/labels/enhancement","name":"enhancement","color":"84b6eb"},{"url":"https://api.github.com/repos/dart-lang/linter/labels/Lint%20Request","name":"Lint Request","color":"006b75"},{"url":"https://api.github.com/repos/dart-lang/linter/labels/style","name":"style","color":"fbca04"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-03-11T09:45:13Z","updated_at":"2015-03-11T13:32:03Z","closed_at":null,"body":"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'.\r\n\r\nThe 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, every `final` in the code simplifies comprehension.\r\n\r\nIn 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`.\r\n\r\nA 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 #45).\r\n"},"comment":{"url":"https://api.github.com/repos/dart-lang/linter/issues/comments/78262827","html_url":"https://github.com/dart-lang/linter/issues/46#issuecomment-78262827","issue_url":"https://api.github.com/repos/dart-lang/linter/issues/46","id":78262827,"user":{"login":"zoechi","id":405837,"avatar_url":"https://avatars.githubusercontent.com/u/405837?v=3","gravatar_id":"","url":"https://api.github.com/users/zoechi","html_url":"https://github.com/zoechi","followers_url":"https://api.github.com/users/zoechi/followers","following_url":"https://api.github.com/users/zoechi/following{/other_user}","gists_url":"https://api.github.com/users/zoechi/gists{/gist_id}","starred_url":"https://api.github.com/users/zoechi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zoechi/subscriptions","organizations_url":"https://api.github.com/users/zoechi/orgs","repos_url":"https://api.github.com/users/zoechi/repos","events_url":"https://api.github.com/users/zoechi/events{/privacy}","received_events_url":"https://api.github.com/users/zoechi/received_events","type":"User","site_admin":false},"created_at":"2015-03-11T13:32:02Z","updated_at":"2015-03-11T13:32:02Z","body":"+1 I use `final` everywhere and only change it to `var` or some concrete type when it should be mutable because of the reason mentioned by @eernstg.\r\nI think to remember that this also helps dart2js to generate more efficient code."}}