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

Rule request: Avoid using the Assignment by Addition Operator (+=) to build a collection #1934

Closed
iRon7 opened this issue Aug 24, 2023 · 2 comments

Comments

@iRon7
Copy link

iRon7 commented Aug 24, 2023

(I thought there was already a request for this or at least discussion that the implementation is difficult as it is hard to define the type of the concerned variable. I think it is possible to come pretty far with just AST)

Summary of the new feature

Array addition is inefficient because arrays have a fixed size. Each addition to the array creates a new array big enough to hold all elements of both the left and right operands. The elements of both operands are copied into the new array. For small collections, this overhead may not matter. Performance can suffer for large collections.
This will create more awareness of the possible performance impact for scripts as e.g. in #1933
See also:

Proposed technical implementation details (optional)

See (PowerShell based) prototype AvoidPlusEqualsToBuildCollections at: https://github.com/iRon7/PSRules

What is the latest version of PSScriptAnalyzer at the point of writing

1.21.0

@iRon7 iRon7 changed the title Rule request: Avoid using the Assignment by Addition Operator to build collections Rule request: Avoid using the Assignment by Addition Operator to build a collection Aug 24, 2023
@iRon7 iRon7 changed the title Rule request: Avoid using the Assignment by Addition Operator to build a collection Rule request: Avoid using the Assignment by Addition Operator (+=) to build a collection Aug 24, 2023
@bergmeister
Copy link
Collaborator

dupe of #806 and #904

@microsoft-github-policy-service
Copy link
Contributor

This issue has been marked as duplicate and has not had any activity for 1 day. It will be closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants