Skip to content

DE0006: Non-generic collections shouldn't be used (especially in Pester tests) #10315

Closed

Description

@ThreeFive-O

As per .NET Platform compatibility rule DE0006 and PowerShell coding guidelines non-generic collections shouldn't be used.

In particular relevant for PowerShell would be System.Collections.ArrayList and System.Collections.Hashtable.

Most occurrences of ArrayList are in C# code obviously, but some can be found in Pester test files too (due to the lack of an internal dynamic array type in PowerShell).

Those two styles are found in Pester tests:

  • New-Object System.Collections.ArrayList
  • [System.Collections.ArrayList]::new()

C# code
The replacement of the non-generic collections in C# code by the generic ones might be non-trivial due to the type problems. For new code and code changes the rule should be followed as the replacement is an on-going process.

Pester tests
For Pester test files the PowerShell syntax should be favored over the explicitly typed C# equivalents:

  • System.Collections.Hashtable -> @{}
  • System.Collections.ArrayList -> @() (though being of type Array; in most cases the performance can be neglected using += operator on @() on very small arrays; some instances might be refactored to use a ForEach loop which returns an Array)

Activity

  1. added
    Issue-Code Cleanup
    on Aug 9, 2019
  2. iSazonov commented on Aug 9, 2019

    @iSazonov
    Collaborator

    PowerShell engine uses ArrayList as base type in PSObject and special variables, also it uses ArrayList for serialization and remoting. Changing this is huge breaking change.

    Nevertheless, there are places where we use ArrayList unjustifiably and this can be fixed. Code becomes more readable and reliable.

  3. KirkMunro commented on Aug 9, 2019

    @KirkMunro
    Contributor

    Also to be clear, DE0006 recommends avoiding the use of non-generic collections in new code. For existing code we can consider the benefits gained from converting to generic collections where possible (i.e. for PowerShell, where such changes would be non-breaking), but that's it.

  4. ThreeFive-O commented on Aug 9, 2019

    @ThreeFive-O
    ContributorAuthor

    Ilya (@iSazonov) and Kirk Munro (@KirkMunro) I agree that the fundamental architecture of PowerShell should stay as it is.

    DE0006 recommends avoiding the use of non-generic collections in new code.
    This is what I meant with on-going replacement.

    Travis Plunk (@TravisEz13) Aditya Patwardhan (@adityapatwardhan) Not sure who is best informed about this, but do Codacy and CodeFactor have rules with an error or warning when using non-generic collections?

    From Ilya (@iSazonov)'s PR #10333 I can't find any hint that would suggest that Codacy or CodeFactor recognized the change from non-generic to generic collections (I would have guessed it should be marked as fixed issue in the logs of Codacy or CodeFactor).

  5. iSazonov commented on Aug 9, 2019

    @iSazonov
    Collaborator

    I have never seen such warnings from Codacy or CodeFactor.

  6. ThreeFive-O commented on Aug 9, 2019

    @ThreeFive-O
    ContributorAuthor

    Ilya (@iSazonov) I guessed so ☹

    I'm not familiar with the tools, but maybe there is the possibility to implement custom rules or have some other tool do the check in the CI pipeline.

  7. vexx32 commented on Aug 9, 2019

    @vexx32
    Collaborator

    We have adjusted Codacy and CodeFactor rules before; I think Travis Plunk (@TravisEz13) has worked with it, or maybe Dongbo Wang (@daxian-dbw)?

  8. TravisEz13 commented on Aug 9, 2019

    @TravisEz13
    Member

    Individual rules are adjusted by configuration files in the repo. See the services web site for instructions, on how to add a configuration.

    Codacy has been disable for C# because it is using an old version of C# for analysis

  9. iSazonov commented on Sep 6, 2019

    @iSazonov
    Collaborator

    Most of ArrayList was replaced. I close this because remaining ArrayList is used in engine and remoting.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Code CleanupResolution-Fixed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions