Static fields naming convention
Supports the 3 most used static field naming conventions (adaptable to your own):
- name starting with an upper-case letter
- name starting with "_" followed by an upper-case letter
- name starting with "s_" followed by an upper-case letter
A static field behaves completely differently from an instance field, so differentiating them at a glance (e.g. s_ prefix) is valuable. Related discussion: https://blog.ndepend.com/on-hungarian-notation-for-fields-in-csharp/
- name starting with an upper-case letter
- name starting with "_" followed by an upper-case letter
- name starting with "s_" followed by an upper-case letter
A static field behaves completely differently from an instance field, so differentiating them at a glance (e.g. s_ prefix) is valuable. Related discussion: https://blog.ndepend.com/on-hungarian-notation-for-fields-in-csharp/
How to fix
After adapting the rule to your naming convention, name all matched static fields adequately.
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>Static fields naming convention</Name>
// <Id>ND2001:StaticFieldsNamingConvention</Id>
warnif count > 0
from f in JustMyCode.Fields where
f.IsStatic &&
!f.IsLiteral &&
!f.IsGeneratedByCompiler &&
!f.IsSpecialName &&
!f.IsEventDelegateObject &&
!(
// Static field name starting with an upper-case letter
(f.Name.Length >= 1 && char.IsLetter(f.Name[0]) && char.IsUpper(f.Name[0])) ||
// Static field name starting with "_" followed with an upper-case letter
(f.Name.Length >= 2 && f.Name.StartsWith("_") && char.IsLetter(f.Name[1]) && char.IsUpper(f.Name[1])) ||
// Static field name starting with "s_" followed with an upper-case letter
(f.Name.Length >= 3 && f.Name.StartsWith("s_") && char.IsLetter(f.Name[2]) && char.IsUpper(f.Name[2]))
)
select new {
f,
f.SizeOfInst,
Debt = 3.ToMinutes().ToDebt(),
Severity = Severity.Medium
}
//<Expl>
// The *static {0}* name does not adhere to usual .NET field naming conventions.
// It does not start with an uppercase letter,
// an underscore followed by an uppercase letter
// or a *s_* expression followed by an uppercase letter.
//</Expl>
//<Desc>
// Supports the 3 most used static field naming conventions (adaptable to
// your own):
//
// - name starting with an upper-case letter
//
// - name starting with "_" followed by an upper-case letter
//
// - name starting with "s_" followed by an upper-case letter
//
// A *static field* behaves completely differently from an *instance field*,
// so differentiating them at a glance (e.g. **s_** prefix) is valuable.
// Related discussion:
// https://blog.ndepend.com/on-hungarian-notation-for-fields-in-csharp/
//</Desc>
//<HowToFix>
// After adapting the rule to your naming convention, name all matched
// static fields adequately.
//</HowToFix>
Open in the interactive Rules Explorer · All default rules
Naming Conventions: all 23 items
- Instance fields naming convention ND2000
- Static fields naming convention ND2001
- Interface name should begin with a 'I' ND2002
- Abstract base class should be suffixed with 'Base' ND2003
- Exception class name should be suffixed with 'Exception' ND2004
- Attribute class name should be suffixed with 'Attribute' ND2005
- Types name should begin with an Upper character ND2006
- Methods name should begin with an Upper character ND2007
- Do not name enum values 'Reserved' ND2008
- Avoid types with name too long ND2009
- Avoid methods with name too long ND2010
- Avoid fields with name too long ND2011
- Avoid having different types with same name ND2012
- Avoid prefixing type name with parent namespace name ND2013
- Avoid naming types and namespaces with the same identifier ND2014
- Don't call your method Dispose ND2015
- Methods prefixed with 'Try' should return a boolean ND2016
- Properties and fields that represent a collection of items should be named Items. ND2017
- DDD ubiquitous language check ND2018
- Avoid fields with same name in class hierarchy ND2019
- Avoid various capitalizations for method name ND2020
- Controller class name should be suffixed with 'Controller' ND2021
- Nested class members should not mask outer class' static members ND2022