Sunday 2 September 2018

C# Coding Standards Series – Naming Conventions

Terminology and Definitions for Naming Conventions:

Camel Case (camelCase): The first letter of the word is lower case and then each first letter of the part of the word is upper case;

Pascal Case (PascalCase): The first letter of the word is upper case and then each first letter of the part of the word is upper case;

Underscode Prefix (_underscore): The word begins with underscore char and for the rest of the word use camelCase rule;

General Rules

1. Use Pascal Case for Class names:
Ex: public class HelloWorld { ... };

2. Use Pascal Case for Method names:
Ex: public void SayHello(string name) { ... };

3. Use Camel Case for variables and method parameters:
Ex: int totalCount = 0;
void SayHello(string name)
{
     string fullMessage = "Hello " + name;
     //...
}

4. Avoid all upper case or all lower case names;

5. Do not use Hungarian notation:
Ex: string m_sName; (the prefix m_ means that is a member variable and s means that is a string data type);

6. Avoid abbreviations longer than 5 characters;

7. Avoid using abbreviations unless the full name is excessive:
Ex: Good: string address;           
    Not good: string addr;

8. Use meaningfull, descriptive words for naming variables;

9. Try to prefix Boolean variables with “Can”, “Is” or “Has”;

10. Do not use Underscore Prefix for local variables names;

11. All member variables must use Underscore Prefix so that they can be identified from other local variables names;

12. Avoid naming conflicts with existing .NET Framework namespaces or types;

13. Do not include the parent class name within a property name:
Ex: Good: Customer.Name;             
    Not good: Customer.CustomerName;

14. When defining a root namespace, use a Product, a Company or a Developer name as the root:
Ex: NorthwindApplication.Utilities;

15. Use Pascal Case for file names;

16. Method name should tell you what it does;

17. A method should do only “one job”. Do not combine multiple jobs in one method even if those jobs have very few lines of code.
Ex:
protected void SaveCustomerName(string customerName)
{
   //code here...
}


Naming Conventions for ASP.NET Controls

In general, naming ASP.NET controls is made using Camel Case naming convention, where the prefix of the name is the abbreviation of the control type name.

AbbreviationASP.NET Control
STANDARD CONTROLS
btnButton
cbCheckBox
cblCheckBoxList
ddlDropDownList
fuFileUpload
hdnHiddenField
lnkHyperlink
imgImage
ibtn(btn)ImageButton
lblLabel
lbtn(btn)LinkButton
lbListBox
litLiteral
mvMultiView
pnlPanel
phPlaceHolder
rbRadioButton
rblRadioButtonList
tblTable
txtTextBox
vView
DATA CONTROLS
dtlDataList
dpDataPager
dtvDetailsView
etsEntityDataSource
fvFormView
gvGridView
ldsLinqDataSource
lvListView
odsObjectDataSource
qeQueryExtender
rptRepeater
smdSiteMapDataSource
sdsSqlDataSource
xdsXmlDataSource
VALIDATION CONTROLS
cpvCompareValidator
ctvCustomValidator
rvRangeValidator
revRegularExpressionValidator
rfvRequiredFieldValidator
vsValidationSummary

No comments:

Post a Comment