Identifiers should have correct suffix (CA1710, Microsoft.Naming)

by Mark Zhou 24. October 2010 21:01

I discussed the name conventions for the different code parts in this article, it contains a table showing the correct casing  convention (Pascal or camel) you should use for your code parts; it also contains the recommended prefix and suffix you should use for the name of an identifier. Particularly, this rule validates the name of the identifier which may contain suffix.

If a type extends certain system base types, it may have a suffix. As I mentioned in that article, the Exception-derived type must have a suffix “Exception” for its name; similarly, any type that extends System.Attribute should have “Attribute” suffix. Suffix can be used to easily understand its base type, in addition, the name of the identifier which represents a variable of a certain user control may contain the suffix for the user control type.

Only types (class, struct, interface, delegate, enum) may have suffix; for other code parts, such as property, method, field, constant, or method, should not have suffix.

Here is a list for the types that derived from certain system base type and may contain suffixes.

Base Type Suffix Example
System.Attribute Attribute CustomValidationAttribute
System.EventArgs EventArgs DoWorkCompletedEventArgs
System.Exception Exception TimeoutException
System.Collections.ICollection or System.Collections.Generic.ICollection<T> Collection ControlCollection
System.Collections.IDictionary or System.Collections.Generic.IDictionary<TKey, TValue> Dictionary ModelStateDictionary
System.Collections.IEnumerable or System.Collections.Generic.IEnumerable<T> Collection ControlCollection
System.Collections.Queue or System.Collections.Generic.Queue<T> Collection or Queue ThreadContextSwitchQueue
System.Collections.Stack or System.Collections.Generic.Stack<T> Collection or Stack ConcurrentStack
System.Data.DataSet DataSet ProductDataSet
System.Data.DataTable Collection or DataTable UserInfoDataTable
System.IO.Stream Stream MemoryStream
System.Security.IPermission Permission SecurityPermission
System.Security.Policy.IMembershipCondition Condition AspNetMembershipCondition
All event handler delegates or System.EventHandler or System.EventHandler<TEventArgs> EventHandler OnButtonClickEventHandler
All user control Base type of the user control SplitButton, AutoCompleteTextBox

Please note: The types in the System.Collections namespace listed in the above table may be used to implement generalized data structures, for example, A System.Collections.Stack can be used to implement a concurrent stack. Sometimes the types in this namespace may be used to implement data structures that based on one of these types and extend the underlying type, for example, a BinaryTree can implement System.Collections.IEnumerable interface, but logically they have no direct relationship. In this case, developer can choose a different meaningful name for this particular type implementation. Many types in Framework Class Library (FCL) implement System.Collections.IEnumerable but they do not have “Collection” suffix, such as System.String, System.Linq.IQueryable or System.Collections.Generic.List<T>.

It is safe to suppress a warning to use the 'Collection' suffix if the type is a generalized data structure that might be extended or that will hold an arbitrary set of diverse items. In this case, a name that provides meaningful information about the implementation, performance, or other characteristics of the data structure might make sense (for example, BinaryTree). In cases where the type represents a collection of a specific type (for example, StringCollection), do not suppress a warning from this rule because the suffix indicates that the type can be enumerated by using a foreach statement.

For other suffixes, do not suppress a warning from this rule. The suffix allows the intended usage to be evident from the type name

To learn more about this rule, see the MSDN link for CA1710 for more details.

Tags: , , ,

Pingbacks and trackbacks (1)+

Add comment




  Country flag
biuquote
  • Comment
  • Preview
Loading


Translate This Page

About Mark

Mark is a developer who works for building base class libraries and tools for developers.

Mark's Awards

Microsoft Community Contributor

Month List

Who visit this site

Recent Comments

Comment RSS