Description
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 typeArray; in most cases the performance can be neglected using+=operator on@()on very small arrays; some instances might be refactored to use aForEachloop which returns anArray)
Activity
iSazonov commentedon Aug 9, 2019
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.
KirkMunro commentedon Aug 9, 2019
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.
ThreeFive-O commentedon Aug 9, 2019
Ilya (@iSazonov) and Kirk Munro (@KirkMunro) I agree that the fundamental architecture of PowerShell should stay as it is.
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).
iSazonov commentedon Aug 9, 2019
I have never seen such warnings from Codacy or CodeFactor.
ThreeFive-O commentedon Aug 9, 2019
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.
vexx32 commentedon Aug 9, 2019
We have adjusted Codacy and CodeFactor rules before; I think Travis Plunk (@TravisEz13) has worked with it, or maybe Dongbo Wang (@daxian-dbw)?
TravisEz13 commentedon Aug 9, 2019
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
iSazonov commentedon Sep 6, 2019
Most of ArrayList was replaced. I close this because remaining ArrayList is used in engine and remoting.