Skip to content
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

[DISABLED] Lint to prefer using a public final field instead of a private field with a public getter (Style Guide) #57169

Closed
pq opened this issue Feb 10, 2015 · 7 comments
Labels
analyzer-linter Issues with the analyzer's support for the linter package area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. linter-lint-request type-enhancement A request for a change that isn't a bug

Comments

@pq
Copy link
Member

pq commented Feb 10, 2015

From the style guide:

PREFER using a public final field instead of a private field with a public getter.

If you have a field that outside code should be able to see but not assign to (and you don’t need to set it outside of the constructor), a simple solution that works in many cases is to just mark it final.

GOOD:

class Box {
  final contents = [];
}

BAD:

class Box {
  var _contents;
  get contents => _contents;
}
@pq pq added the type-enhancement A request for a change that isn't a bug label Feb 10, 2015
@zoechi
Copy link
Contributor

zoechi commented Feb 18, 2015

This rule makes only sense when the field is set directly from an argument passed in with every constructor (this.xxx or constructor initialzer list). Otherwise the linter just complaints about every field which is read-only in the public interface but writeable in the private interface. I can't see anything bad about it in the later case.

@pq
Copy link
Member Author

pq commented Feb 18, 2015

@zoechi 👍 In fact, the advice "Make it final" would be inapplicable (since it wouldn't compile).

@pq
Copy link
Member Author

pq commented Feb 18, 2015

Opened corresponding bug: #57183.

@pq
Copy link
Member Author

pq commented Mar 16, 2015

Disabled pending fix of #57183.

@pq pq changed the title Lint to prefer using a public final field instead of a private field with a public getter (Style Guide) [DISABLED] Lint to prefer using a public final field instead of a private field with a public getter (Style Guide) Mar 16, 2015
@xxgreg
Copy link

xxgreg commented Mar 17, 2015

One way you could make this lint simpler - and not have to scan the entire library for member access is:

  • Make the lint optional, and disabled by default.
  • Introduce an annotation @shared.
  • When the lint is enabled, assume all members that aren't annotated with shared are class private.
  • Don't lint members annotated as shared.

You could also add another lint which checks for accidental usage of class private members. (i.e. members which are not annotated as shared)

I'm not sure if this level of strictness would be useful practise. But I'm sure some people, especially those coming from C# or Java would like to use it.

Writing helper code to scan an entire library for member usage will be helpful for other lints, so in the long run it's probably worth doing this correctly anyway.

@hankyusa
Copy link

hankyusa commented Mar 3, 2021

I didn't realize this issue existed when I created dart-lang/site-www#2971.

@pq
Copy link
Member Author

pq commented Jan 19, 2023

If we want to pursue this, let's start with a fresh proposal.

@pq pq closed this as completed Jan 19, 2023
@devoncarew devoncarew added analyzer-linter Issues with the analyzer's support for the linter package area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. labels Nov 18, 2024
@devoncarew devoncarew transferred this issue from dart-lang/linter Nov 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer-linter Issues with the analyzer's support for the linter package area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. linter-lint-request type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

5 participants