|
Identifier Type
|
Rules For Naming
|
Example
|
|
Namespaces
|
Namespace names should be nouns, in Pascal case. Avoid the use of underscores ("_")
in namespace names. Try to keep names simple and descriptive. Use whole words and
avoid acronyms and abbreviations unless the abbreviation is much more widely used
than the long form,
such as
Url or Html. All custom namespace names are to begin with the company name if applicable,
followed by the
project, product, or technology name, and the purpose name, followed by the
purpose of the package as an
organizational unit.
|
MyCompany.Framework.Data;
MyCompany.Factories;
|
|
Classes
|
Class names
should be nouns, in Pascal case. As with namespaces, keep class names simple and
descriptive. Use whole words and avoid acronyms and abbreviations unless the abbreviation
is much more widely used than the long form, such as Url or Html.
|
class SalesOrder;
class LineItem;
class HtmlWigets;
|
|
Interfaces
|
Interface names use Pascal case and begin with the letter "I".
|
interface IBusinessRule;
|
|
Methods
|
Methods should be active verb/nouns forms, in Pascal case.
|
GetDataRow();
UpdateOrder();
|
|
Instance Fields
|
Instance fields are in camel case.
Variable
names should not start with underscore, even though it is allowed.
Variable names should be meaningful.
The choice of a variable name should
be mnemonic – that is, designed to indicate to the casual observer the intent of
its use. One-character variable names
should be avoided.
|
protected string name;
private int orderId;
private string lastName;
private float width;
// AVOID!
private _total;
|
|
Enum Types
and Enum
Values
|
Enum types and values
are in Pascal case. Use abbreviations sparingly.
Do not use an Enum
suffix on Enum type names. Use a singular
name for most Enum types, but use a
plural name for Enum types that are
bit fields.
|
enum Status {ReadyToGo,
WaitingForNow};
enum Day {Monday, Tuesday};
|
|
Events
|
Events are in Pascal case. Use the suffix “EventHandler” on event handler names.
Specify two parameters named sender and
e.
The
sender
parameter represents the object that raised the event. The
sender
parameter is always of type object, even if it is possible to use a more specific
type.
The state associated with the event is encapsulated in an instance of an event class
named
e.
Use an appropriate and specific event class for the e
parameter type. Name an event argument class with the EventArgs suffix. Consider
naming events with a verb. Use a gerund (the "ing" form of a verb) to create an
event name that expresses the concept of pre-event, and a past-tense verb to represent
post-event. For example, a Close event
that can be canceled should have a Closing event and a
Closed
event. Do not use the BeforeXxx/AfterXxx naming pattern.
Do not use a prefix or suffix on the
event declaration on the type. For
example, use
Close
instead of
OnClose.
In general, you should provide a protected method called
OnXxx
on types with events that can be overridden in a derived class. This method should
only have the event parameter e, because the sender
is always the instance of the type.
|
public delegate void
MouseEventHandler(object sender,
MouseEventArgs e);
|
|
Exception
Class
|
Exception classes are in Pascal case
and always have the suffix “Exception”.
|
InvalidCastException
DomainValueException
|
|
Custom
Attributes
|
Custom attributes are in Pascal case
and always have the suffix “Attribute”.
|
PersistentEntityAttribute
|
|
Properties
|
Properties are in Pascal case with an. Property names should directly reflect the
underlying attribute.
|
public int OrderId
public string LastName
|
|
Object References
|
Objects references are camel case
when non-public and pascal case when public (although references should never be
public without good reason). Except where exceptionally warranted, objects are named
after their class. An exception to this rule would be when two or more objects are
needed of the same class within the same scope.
Avoid naming object references as
abbreviations or acronyms of the class name.
|
public string Name;
Order order = new Order;
LineItem lineItem =
new LineItem();
|
|
Constants
|
Constants are in Pascal case. They
should not be all uppercase with words separated by underscores "_".
|
const int NumDaysInWeek = 4;
// AVOID!
const int NUM_DAYS_IN_WEEK = 4;
|