Avoid non-readonly static fields
Warns about static fields not declared read-only. Instance fields are the natural OOP artifact for mutable state; mutable static fields create confusion about runtime state and impair testability (the same mutable state is re-used per test). More: https://blog.ndepend.com/the-proper-usages-of-the-keyword-static-in-c/
How to fix
If assigned once per program lifetime, declare the field readonly and assign it inline or in the static constructor. Otherwise refactor to hold the state in an instance field.
CQLinq source
This rule is written in CQLinq, C# LINQ over the NDepend code model. It ships with every new NDepend project: you can read it, adapt it, or use it as a starting point for your own custom rules.
// <Name>Avoid non-readonly static fields</Name>
// <Id>ND1901:AvoidNonReadOnlyStaticFields</Id>
warnif count > 0
from f in JustMyCode.Fields
where f.IsStatic &&
!f.IsEnumValue &&
!f.IsGeneratedByCompiler &&
!f.IsLiteral &&
!f.IsInitOnly
let methodAssigningField = f.MethodsAssigningMe
select new {
f,
methodAssigningField,
Debt = (2+8*methodAssigningField.Count()).ToMinutes().ToDebt(),
Severity = Severity.High
}
//<Expl>
// The *static {0}* should be declared as *readonly* because changing it can lead to runtime confusion and hinder code testability.
// In *Object-Oriented Programming*, *instance fields* are the preferred choice for holding modifiable states.
//</Expl>
//<Desc>
// Warns about static fields not declared read-only. **Instance fields** are
// the natural OOP artifact for mutable state; mutable static fields create
// *confusion* about runtime state and impair testability (the same mutable
// state is re-used per test). More:
// https://blog.ndepend.com/the-proper-usages-of-the-keyword-static-in-c/
//</Desc>
//<HowToFix>
// If assigned once per program lifetime, declare the field *readonly* and
// assign it inline or in the static constructor. Otherwise refactor to hold
// the state in an instance field.
//</HowToFix>
Open in the interactive Rules Explorer · All default rules
Immutability: all 15 items
- Fields should be marked as ReadOnly when possible ND1900
- Avoid non-readonly static fields ND1901
- Avoid static fields with a mutable field type ND1902
- Structures should be immutable ND1903
- Immutable struct should be declared as readonly ND1914
- Property Getters should be pure ND1904
- A field must not be assigned from outside its parent hierarchy types ND1905
- Don't assign a field from many methods ND1906
- Do not declare read only fields with mutable reference types ND1907
- Public read only array fields can be modified ND1908
- Types tagged with ImmutableAttribute must be immutable ND1909
- Types immutable should be tagged with ImmutableAttribute ND1910
- Methods tagged with PureAttribute must be pure ND1911
- Pure methods should be tagged with PureAttribute ND1912
- Record should be immutable ND1913