C# Coding Solutions—Class and Interface Identifiers

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
CSharp-Online.NET:Articles
C# Articles

C# Coding Solutions

© 2006 Christian Gross

Class and Interface Identifiers

Using the class identifier in conjunction with the namespace identifier is an easy way to quickly identify the class’s purpose. In general a class identifier should represent the functionality that it is implementing. A class identifier should be a noun, except in the case of helper and disposal classes, which can be verbs. The name of the class should not be abbreviated unless the abbreviation is common (such as HTTP).

Sometimes the name of the class is related to a general programming terminology. Typically you identify such classes by appending the general programming term to the name of the class. So if the general programming term were interface implementation, and the class name were FileReader, then the new name would be FileReaderImplementation. The following is a list of general programming terms and how they are used. (A text identifier within square brackets represents the class identifier, such as FileReader.)

Exception—[identifier]Exception

This is used to throw an exception, which signals that something went wrong. There are two types of exceptions: fatal and nonfatal. A fatal exception occurs when the application does something that forces it to exit. A nonfatal exception happens when something goes wrong and processing stops, but the application can continue executing. In the following example, ConfigurationDoesNotExistException is a fatal exception, and FileIsCorruptException is a nonfatal exception:

public class 
    ConfigurationDoesNotExistException : Exception { }
public class 
    FileIsCorruptException : ApplicationException { }

Test[identifier]Test | [identifier]TestSuite | [identifier]TestClass | [identifier]TestCase

The multiple identifiers here used to identify classes that implement testing functionality. You can use Test, TestClass, or TestCase to identify a single set of tests for a unit test. The identifier TestSuite can identify a suite of tests that is composed of tests, test cases, or test classes.

DefaultDefault[identifier]

Typically a class uses the Default identifier when the class is defined to be a default implementation of an interface. The Default identifier is prefixed to the class identifier, and does not have a prefix. A default implementation is necessary when defining an interface, because you should not use null values in code that uses interfaces.

Delegate[identifier]Delegate

This identifies a type that represents a .NET delegate. When you use the delegate keyword, you append the identifier with Delegate.

Algorithm[identifier]Builder | [identifier]Compiler | [identifier]Parser, etc.

A class or interface with the keyword Builder, Compiler, Parser or any word that is a verb and has an &quor;er&quor; ending processes data in a fairly sophisticated algorithm. Routines move data from one object to another. These types of routines involve only the assigning of data. But the more-complex routines used here consume a larger amount of data, perform some analysis, and then spit out an answer. Typically such routines will parse data looking for certain tokens or assemble a more complex tree structure.

Handler[identifier]Handler

The Handler class type is a specific type of class that deserves its own class-type identification. The Handler class types serves as a callback mechanism when a piece of code is making asynchronous requests.

Source[identifier]Provider

A Provider class type is very special because it is a class or interface that provides a bridge to another resource. It is a source of data that the provider’s user consumes. Database access classes are common providers.

Navigation[identifier]Child | [identifier]Parent, [identifier]Manager, etc.

Navigation identifiers are very common in component structures. The Navigation identifier is not limited to Child, Parent, or Manager, but could include Sibling, Descendent, and so on. Additionally, Navigation does not need to be restricted to class or interface identifiers. With Navigation identifiers, methods can be used as identifiers. Navigation identifiers let you construct a complex data structure that encompasses many different objects. Typically a class or interface of Navigation type is a bridge that combines several references into a useful structure. Navigation identifiers let you move from one part of the data structure to another.

Data[identifier]Data

A data type contains data members only. The type is typically defined as structure, but can be class type. A data type usually focuses on storing data, and any methods or properties are meant to aid data manipulations. A data type does not have methods or properties that implement business logic.

Utility[identifier]Utils

The Utility class type is common, and provides basic runtime support. A Utility class is often called a helper class because it provides a single piece of functionality. The class itself is not complete, but it implements some complete business logic.

A Utility class attempts to encapsulate some common programmatic steps into a simple easy-to-use class. The following source code illustrates the Utility class:

public class FactoryUtils {
    public FactoryUtils() {
    }
    public static Factory ExceptionFactory() {
    }
    public static Factory NullFactory() {
    }
    public static Factory ConstantFactory(
         Object constantToReturn) {
    }
    public static Factory reflectionFactory(
         Class classToInstantiate) {
    }
}

In the code example the class FactoryUtils provides four functions to instantiate the interface Factory based on the choice of instantiation process. The methods encapsulate complexity that would have to be written over and over again. A Utility class is typically an implementation of the Façade pattern. The implementation of the class FactoryUtils contains quite a bit of source code to perform the operation. However, the class FactoryUtils hides the complexity.

Structural[identifier]Support | [identifier]Base

The Structural class type is a base class or support class that provides functionality so that a developer does not have to implement the same functionality over and over. In .NET terms, a structural class is typically an abstract class that implements an interface. The structural class is defined as an abstract class because a subset of interface functionality is implemented. A structural class typically cannot execute on its own; it comes into play when the Template pattern is implemented.

Collection[identifier]List | [identifier]Map | [identifier]Bag | [identifier]Set | [identifier]Collection

The Collection class type is a class that is used to manage a collection of objects. The objects can be associated or arranged using different techniques, and therefore various identifiers are used for various collection types.

Constants[identifier]Constant(s)

The Constants type defines a class that contains a number of constants used throughout a piece of source code. The type being named might also be a data member.

Type: Interface—I[identifier]

In .NET, interfaces are usually prefixed with a capital I. I am a bit uneasy with this because it breaks all of the other naming conventions, and could easily be replaced with [identifier]Interface. Be that as it may, you should follow the accepted convention and prefix an I.

Remember the following points when writing code that uses a naming convention.

  • A good namespace is indispensable when code needs to be maintained, extended, and shared with other developers.
  • A well-thought-out namespace is crucial if your code will reside in an assembly that is subject to strong naming conventions.
  • Namespaces have a parent-child relationship, where a child namespace contains specializations or implementations defined in the parent namespace.
  • Devote some time to naming classes, interfaces, and so on—doing so makes it easier for you and others to find and understand a piece of code.


Previous_Page_.gif Next_Page_.gif


Personal tools