Powershell: Naming convention for `internal static readonly`

Created on 27 May 2020  Â·  6Comments  Â·  Source: PowerShell/PowerShell

In #11777, the readonly modifier was added to internal static members which raised an issue of our naming convention.

According to StyleCopAnalyzers on CodeFactor, "readonly variables that are not declared private must start with an upper-case letter" otherwise there are violations of the rule SA1304NonPrivateReadonlyFieldsMustBeginWithUpperCaseLetter

Our naming conventions state static fields should start with s_, however the existing code overwhelmingly uses PascalCase for internal static readonly members.

In my view conventions should be descriptive rather than prescriptive, so we should consider making changes to the written naming convention.

Area-Maintainers-Documentation Issue-Question

Most helpful comment

https://github.com/dotnet/codeformatter/issues/281#issuecomment-442733951 makes a case that readonly should not change the casing style or prefixes.

I don't have a strong opinion either way, but CodeFactor rules should not conflict with naming conventions – it adds too much friction to pull requests.

All 6 comments

Yeah most of the newer code I see uses PascalCase for these. Might be that our naming conventions doc needs an update? /cc @SteveL-MSFT

https://github.com/dotnet/codeformatter/issues/281#issuecomment-442733951 makes a case that readonly should not change the casing style or prefixes.

I don't have a strong opinion either way, but CodeFactor rules should not conflict with naming conventions – it adds too much friction to pull requests.

Currently, the PowerShell naming convention states:

Use _camelCase to name internal and private fields and use readonly where possible.
Prefix instance fields with _, static fields with s_ and thread static fields with t_.

Unfortunately there is no StyleCop rule available to enforce the use of _camelCase for internal.

I think the following changes would be reasonably descriptive of the existing codebase:

  • Use _camelCase to name private fields (and prefix instance fields with _, static fields with s_ and thread static fields with t_).
  • Use PascalCase to name non-private fields.

If we modified the written convention as suggested we could enforce this using the StyleCop rule:
SA1307: AccessibleFieldsMustBeginWithUpperCaseLetter.

We should also consider: https://github.com/dotnet/runtime/blob/master/docs/coding-guidelines/coding-style.md

GitHub
.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps. - dotnet/runtime

/cc @adityapatwardhan @daxian-dbw @TravisEz13 Thoughts?

If we want to modify our code style, we should modify our code style doc. We can discuss it there. I would suggest one issue at a time and not changing any existing rule without a good reason. the main reason for them is internal consistency. cc @daxian-dbw

Was this page helpful?
0 / 5 - 0 ratings