Properties and fields that represent a collection of items should be named Items.
Properties and fields typed with a collection of items should be named with the plural form of Items (e.g. NewDirectories, Words, Values), for readability and predictability.
No warning when any word in the identifier ends with s (TasksToRun, KeysDisabled, ListOfElementsInResult… are valid), nor when the identifier contains Empty (common pattern for a shared immutable empty collection).
Collection types are those implementing IEnumerable<T>, except:
- Non generic types: rarely seen as collections (e.g. System.String implements IEnumerable<char> but strings are named like FirstName).
- Dictionaries: mappings rather than collections, commonly suffixed Map, Table or Dictionary (though names like PersonsByNames often satisfy this rule anyway).
No warning when any word in the identifier ends with s (TasksToRun, KeysDisabled, ListOfElementsInResult… are valid), nor when the identifier contains Empty (common pattern for a shared immutable empty collection).
Collection types are those implementing IEnumerable<T>, except:
- Non generic types: rarely seen as collections (e.g. System.String implements IEnumerable<char> but strings are named like FirstName).
- Dictionaries: mappings rather than collections, commonly suffixed Map, Table or Dictionary (though names like PersonsByNames often satisfy this rule anyway).
How to fix
Rename by making plural the word best describing the items:
- ListOfDir can be renamed Directories.
- Children can be renamed ChildrenItems
- QueueForCache can be renamed QueueOfItemsForCache
- ListOfDir can be renamed Directories.
- Children can be renamed ChildrenItems
- QueueForCache can be renamed QueueOfItemsForCache
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>Properties and fields that represent a collection of items should be named Items.</Name>
// <Id>ND2017:PropertiesAndFieldsThatRepresentACollectionOfItemsShouldBeNamedItems</Id>
warnif count > 0
let collectionTypes = Types.Where(t =>
t.Implement("System.Collections.Generic.IEnumerable<T>".AllowNoMatch()) &&
t.IsGeneric &&
t.FullName != "System.Collections.Generic.IDictionary<TKey,TValue>" &&
!t.Implement("System.Collections.Generic.IDictionary<TKey,TValue>".AllowNoMatch()) &&
t.FullName != "System.Collections.Generic.IReadOnlyDictionary<TKey,TValue>" &&
!t.Implement("System.Collections.Generic.IReadOnlyDictionary<TKey,TValue>".AllowNoMatch())).ToHashSetEx()
let properties = from p in JustMyCode.Properties
where p.PropertyType != null && collectionTypes.Contains(p.PropertyType)
select new { Member = (IMember)p, Type = p.PropertyType }
let fields = from f in JustMyCode.Fields
where f.FieldType != null && collectionTypes.Contains(f.FieldType)
select new { Member = (IMember)f, Type = f.FieldType }
from pair in properties.Concat(fields)
let identifier = pair.Member.SimpleName
where !identifier.EndsWith("s") &&
!identifier.Contains('<') // Remove potential generated fields like backing fields, and also potential generated properties accessors
let words = identifier.GetWords()
where !words.Any(word =>
word.EndsWith("s") || // Don't warn if any word ends with an s
word == "Empty") // Don't warn if any word in the identifier is Empty
select new { pair.Member, pair.Type }
//<Expl>
// The *{0}* is typed with the *collection {1}*.
// Its name should contain the plural form of *Items*
// in the collection to enhance code readability.
//</Expl>
//<Desc>
// Properties and fields typed with a collection of *items* should be named
// with the plural form of *Items* (e.g. *NewDirectories*, *Words*,
// *Values*), for readability and predictability.
//
// No warning when any word in the identifier ends with *s* (**TasksToRun**,
// **KeysDisabled**, **ListOfElementsInResult**… are valid), nor when the
// identifier contains **Empty** (common pattern for a shared immutable
// empty collection).
//
// Collection types are those implementing *IEnumerable<T>*, except:
//
// - Non generic types: rarely seen as collections (e.g. *System.String*
// implements *IEnumerable<char>* but strings are named like *FirstName*).
//
// - Dictionaries: mappings rather than collections, commonly suffixed
// *Map*, *Table* or *Dictionary* (though names like *PersonsByNames*
// often satisfy this rule anyway).
//</Desc>
//<HowToFix>
// Rename by making plural the word best describing the *items*:
//
// - **ListOfDir** can be renamed **Directories**.
//
// - **Children** can be renamed **ChildrenItems**
//
// - **QueueForCache** can be renamed **QueueOfItemsForCache**
//</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