A
comprehensive coding standard is essential for a successful product
delivery. The standard helps in enforcing best practices and avoiding
pitfalls, and makes knowledge dissemination across the team easier. The
C# coding standard presented here is very thin on the “why” and very
detailed on the “what” and the “how.” The coding standard presented
next captures best practices, dos and don'ts, pitfalls, guidelines, and
recommendations, as well as naming conventions and styles, project
settings and structure, and framework-specific guidelines.
Naming Guidelines
1). Private Variables (Fields in C#) Naming Guidelines
Prefix private variables with a
"_" and Hungarian-style notation.
Use camel case as a general rule, or
uppercase for very small words
Example:
_strFirstName, _dsetEmployees
// Field
private OleDbConnection _connection;
// Property
public OleDbConnection Connection
{
get { return _connection; }
set { _connection = value; }
}
2). Local Variables Naming Guidelines
Prefix private or local variables
with Hungarian-style notation.
Use camel case as a general rule, or
uppercase for very small words
Example:
strFirstName, dsetEmployees
3). Namespace Naming Guidelines
The general rule for naming
namespaces is to use the company name followed by the technology name and
optionally the feature and design as follows:
CompanyName.TechnologyName[.Feature][.Design]
Prefixing namespace names with a
company name or other well-established brand avoids the possibility of two
published namespaces having the same name. Use a stable, recognized technology
name at the second level of a hierarchical name.
Example:
Akadia.Traffic,
System.Web.UI, System.Windows.Forms
Use Pascal case as a general rule, or
uppercase for very small words.
Example:
System.Windows.Forms,
System.Web.UI
4). Class Naming Guidelines
Use a noun or noun phrase to name a
class.
Do not use a type prefix, such as C for class, on a class name.
Do not use the underscore character (_).
Use Pascal case. Example:
FileStream, Button
5). Interface Naming Guidelines
Prefix interface names with the
letter "I", to indicate that the type is an interface.
Do not use the underscore character (_).
Use Pascal case. Example:
IServiceProvider,
IFormatable
6). Parameter Naming Guidelines
Use descriptive parameter names.
Parameter names should be descriptive enough that the name of the parameter and
its type can be used to determine its meaning in most scenarios. To distinguish
parameters from other variables the prefix "p" should be used.
Do not prefix parameter names with
Hungarian type notation.
Do not use a prefix for parameter
names of an event handler and exceptions.
Use camel case. Example:
pTypeName,
pNumberOfItems
7). Method Naming Guidelines
Use verbs or verb phrases to name
methods.
Use Pascal case. Example:
RemoveAll(), GetCharAt()
8). Property / Enumerations Naming Guidelines
Use a noun or noun phrase to name
properties.
Do not use Hungarian notation.
Use Pascal case. Example:
BackColor, NumberOfItems
9). Event Naming Guidelines
Use an EventHandler suffix 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.
Use Pascal case. Example:
public delegate void
MouseEventHandler(object sender, MouseEventArgs
e);
9). Exception Naming Guidelines
Event handlers in Visual Studio .NET
tend to use an "e" parameter for the event parameter to the call. To
ensure we avoid a conflict, we will use "ex" as a standard
variable name for an Exception object.
Example
catch (Exception ex)
{
// Handle Exception
}
10). Constant Naming Guidelines
The names of variables declared class
constants should be all uppercase with words separated by underscores. It is
recommended to use a grouping naming schema.
Example (for group AP_WIN):
AP_WIN_MIN_WIDTH,
AP_WIN_MAX_WIDTH, AP_WIN_MIN_HIGHT, AP_WIN_MAX_HIGHT
This mail is by Sanket.
You can also share your information with us at http://www.agenext.com/SuggestTopic.aspx
Be the first to rate this post
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5