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

proposal: avoid_add_all_with_singleton_collection #5094

Open
5 tasks
natebosch opened this issue Sep 13, 2024 · 2 comments
Open
5 tasks

proposal: avoid_add_all_with_singleton_collection #5094

natebosch opened this issue Sep 13, 2024 · 2 comments
Labels
lint-proposal P3 A lower priority bug or feature request status-pending type-enhancement A request for a change that isn't a bug

Comments

@natebosch
Copy link
Member

natebosch commented Sep 13, 2024

avoid_add_all_with_singleton_collection

Description

Call add(foo) over addAll([foo]).

Details

Give a detailed description that should provide context and motivation. Ideally this could be used directly in the rule's documentation.

Kind

Style. Potentially performance with the unnecessary list allocation.

Bad Examples

collection.addAll([a]);

Good Examples

collection.add(a);

Discussion

This has around 1.5k occurrences (when checked by regex) internally. The number is likely much smaller if we only consider collection types from the SDK.

One potential downside to this lint is the editing cliff when going from 1 to multiple items, or multiple to 1 item.

The other factor that may make this not worthwhile, is whether we would apply it to the ListBuilder interface from package:built_collection. Internally I see this pattern used on ListBuilder quite a bit - and that class does not implement any interface from the SDK.

Discussion checklist

No prior discussion I can find.

  • List any existing rules this proposal modifies, complements, overlaps or conflicts with.
  • List any relevant issues (reported here, the SDK Tracker, or elsewhere).
  • If there's any prior art (e.g., in other linters), please add references here.
  • If this proposal corresponds to Effective Dart or Flutter Style Guide advice, please call it out. (If there isn't any corresponding advice, should there be?)
  • If this proposal is motivated by real-world examples, please provide as many details as you can. Demonstrating potential impact is especially valuable.
@natebosch
Copy link
Member Author

@davidmorgan I am curious for your thoughts on the utility of this lint. Do you think it would be better ROI to implement this internally and target built value?

@srawlins srawlins added the type-enhancement A request for a change that isn't a bug label Sep 27, 2024
@davidmorgan
Copy link
Contributor

Thanks Nate!

I looked at internal usage a bit, I noticed there are also a surprisingly large number of addAll([]), which if we follow the same reasoning, should be banned altogether.

On style I think add(foo) and addAll([foo]) are approximately even, I could prefer one or the other depending on context.

I'm hesitant about adding a lint that's supposed to improve performance, I think we'd need to do quite a bit of work to prove that it's worthwhile, e.g. fix across a real app and compare. The biggest downside to linting for performance would be that most violations are probably irrelevant for performance, you probably only care about a few violations that are on hot codepaths.

So broadly my guess is this is probably not worth pursuing, if there turn out to be large performance numbers involved I could be persuaded :)

Thanks.

@natebosch natebosch added the P3 A lower priority bug or feature request label Oct 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lint-proposal P3 A lower priority bug or feature request status-pending type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

3 participants