Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

stephentoub
Copy link
Member

@stephentoub stephentoub commented Jan 8, 2023

The analyzer recommends replacing uses of types with other types that could reduce overheads (e.g. List<T> instead of IList<T>). This fixes most of the violations we currently have of the rule, but it doesn't enable it yet because there are too many false positives; fixes for those are in-flight. Some of these replacements won't help with perf (e.g. using ArgumentException instead of Exception as the return type of a throw helper create method), but there's little harm to them, and some of them do avoid overheads like allocation (e.g. foreach'ing something typed as Dictionary<> instead of IDictionary<> will avoid an enumerator allocation, accessing Count on a List<T> instead of IList<T> will avoid an interface dispatch, etc.)

cc: @buyaa-n, @geeknoid

@ghost ghost added the area-Meta label Jan 8, 2023
@ghost ghost assigned stephentoub Jan 8, 2023
@ghost
Copy link

ghost commented Jan 8, 2023

Tagging subscribers to this area: @dotnet/area-meta
See info in area-owners.md if you want to be subscribed.

Issue Details

The analyzer recommends replacing uses of types with other types that could reduce overheads (e.g. List<T> instead of IList<T>). This fixes most of the violations we currently have of the rule, but it doesn't enable it yet because there are too many false positives; fixes for those are in-flight. Some of these replacements won't help with perf (e.g. using ArgumentException instead of Exception as the return type of a throw helper create method), but there's little harm to them, and some of them do avoid overheads like allocation (e.g. foreach'ing something typed as Dictionary<> instead of IDictionary<> will avoid an enumerator allocation, accessing Count on a List<T> instead of IList<T> will avoid an interface dispatch, etc.)

Author: stephentoub
Assignees: stephentoub
Labels:

area-Meta

Milestone: -

@geeknoid
Copy link
Member

geeknoid commented Jan 8, 2023

Wow, that's a lot of stuff.

CA1859 currently only recommends changing the signature of private methods and fields, the idea being that the changes are completely local to a given type. Do you think it would be useful to extend the analysis to internal symbols too, at least optionally? I bet that would raise another crop of these recommendations.

Another thing I'd like to add in the analyzer is a basic understanding of collections. So if you have an List<IFoo> but you're always assigning Foo instances to it, it would recommend upgrading to a List<Foo>. I think that would be another frequent hit.

The analyzer recommends replacing uses of types with other types that could reduce overheads (e.g. `List<T>` instead of `IList<T>`).  This fixes most of the violations we currently have of the rule, but it doesn't enable it yet because there are too many false positives; fixes for those are in-flight.  Some of these replacements won't help with perf (e.g. using `ArgumentException` instead of `Exception` as the return type of a throw helper create method), but there's little harm to them, and some of them do avoid overheads like allocation (e.g. foreach'ing something typed as `Dictionary<>` instead of `IDictionary<>` will avoid an enumerator allocation, accessing `Count` on a `List<T>` instead of `IList<T>` will avoid an interface dispatch, etc.)
@stephentoub
Copy link
Member Author

CA1859 currently only recommends changing the signature of private methods and fields, the idea being that the changes are completely local to a given type. Do you think it would be useful to extend the analysis to internal symbols too, at least optionally?

Can you share an example?

Another thing I'd like to add in the analyzer is a basic understanding of collections.

Yeah, I think that would be useful, at least for some of the most common, like IEnumerable and List.

Copy link
Contributor

@buyaa-n buyaa-n left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a few NITs, overall LGTM, thank you!

@stephentoub stephentoub merged commit 210a7a9 into dotnet:main Jan 12, 2023
@stephentoub stephentoub deleted the ca1859 branch January 12, 2023 19:58
@geeknoid
Copy link
Member

Be gone Peanut Butter!

@ghost ghost locked as resolved and limited conversation to collaborators Feb 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants