Visualize Systems Ltd - for all your ASP, ASP.Net, Intranet, Internet web development needs Skip to main content
homeprojects and clientsservicesresources
site informationcontact information
resources
Summary

Publications

Latest

Resources - ASP.Net Coding Standards

Adopting a common coding standard across your web applications

Note: These standards assume C# but are just as applicable to VB etc. All these standards stay as close as possible to those recommended by Microsoft.

The Microsoft's Design Guidelines for Class Library Developers (MSDN site) is a useful set of guidelines which all ASP.Net developers should take a look at. In particular, those new to C# coding should see the naming conventions section, including capitalisation conventions (Pascal vs camel etc) and when to use nouns, verbs etc for naming of identifiers. It is interesting to note that Microsoft are moving away from the hungarian style of notation in .Net (I think based on the premise that everything is an object anyway in the framework and identifier names should be based on semantics not types).

Although Microsoft's guidelines are an excellent guide, I created this standards page, as I could not find a one-stop summary of standards I could follow quickly when building ASP.Net web applications - Microsoft's are not exactly a quick reference guide! Also, they do not target ASP.Net specifics (e.g. Web Control naming).

Naming and Capitalisation

Below summarises the naming recommendations for identifiers in .Net. Pascal casing is used mainly (ie capitalise first letter of each word) with camel casing (capitalise each word except for the first one) being used in certain circumstances.

Identifier Case Examples Additional Notes Detailed Info
Class Pascal Person, BankVault, SMSMessage, Dept Class names should be based on "objects" or "real things" and should generally be nouns.
MSDN site - more details
Method Pascal GetDetails, UpdateStore Methods should use verbs or verb phrases.
 
Parameter camel personName, bankCode  
MSDN site - more details
Interface Pascal with "I" prefix IDisposable One of the few times when prefixing is adopted.
MSDN site - more details

Property

Associated private member variable

 

Pascal

_camelCase

 

ForeColor, BackColor

_foreColor, _backColor

 

Use underscore camel casing for the private member variables, but keep the names the same as associated property accessors.
MSDN site - more details
Exception Class Pascal with "Exception" suffix WebException, SMSException  
 
Event Pascal plus optional "EventHandler" suffix where relevant btnSubmit_Click, Painting, Click, Clicked, MyEventHandler VS uses underscores to separate an object from its event. Be careful with tense (pre/past), e.g. a Close event that can be canceled should have a Closing event and a Closed event. MS also recommend adding the "EventHandler" suffix where thought needed.
MSDN site - more details

Note that acronym parts of phrases may be all in uppercase (e.g. SMSMessage) where relevant.

User interface objects (text boxes, drop down lists etc) are treated as a special case and discussed in the next section.

User Interface Objects and Controls

The Microsoft .Net guidelines do not go into detail on the naming of user interface objects (again probably because they are only objects after all), but one approach that is well adopted is the use of the "control type" prefix convention (similar to hungarian style often used in VB, C++ etc). Although this should not be used for indicating variable types, it is useful for user interface controls as it makes it explicitly clear where user interface elements are in your code.

Some samples are below for ASP.Net web form controls:

Control Prefix Example
Label lbl lblSurname
TextBox txt txtSurname
DataGrid dg dgResults
Button btn btnSave
ImageButton ibtn ibtnSave
Hyperlink lnk lnkHomePage
DropDownList ddl ddlCompany
ListBox lst lstCompany
DataList dlst dlstAddress
Repeater rep repSection
Checkbox chk chkMailList
CheckBoxList chk chkAddress
RadioButton rdo rdoSex
RadioButtonList rdo rdoAgeGroup
Image img imgLogo
Panel pan panSection
PlaceHolder plh plhHeader
Calender cal calMyDate
Adrotator adr adrBanner
Table tbl tblResults
[All] Validators val valCreditCardNumber
ValidationSummary vals valsErrors
     

Namespaces

All namespace should use Pascal casing and be prefixed with your company (and department if you wish). See examples below:

Sample "project level" namespaces:

YourCompany.YourDept.YourProject
Visualize.Blog

Generic reusable routines, classes etc which will be used across projects, can sit at the 'company' or 'dept' level, for example:

Visualize.Utils

Commenting Code

Code should be commented where necessary and C# XML commenting tags used, to aid VS intellisense, object browsing and on-line documentation generation if needed. Example: some sample C# source is given below for a method in an SMS tool I developed recently:

/// <summary>
/// SendSMSMessage sends a text message to a valid mobile number
/// </summary>
/// <param name="mobileNo">Mobile number to send to in format +44 1234 112233</param>
/// <param name="message">Message itself, max 160 chars</param>
/// <returns>Message result string from gateway</returns>
public string SendSMSMessage(string mobileNo,string message)
{

// rest of code here...

Full details of more XML tags can be found in the Visual Studio.Net on-line help.

 

 

 

 

Sponsors


home | projects/clients | services | resources | site info | contact us
Copyright Visualize Systems Ltd
 
© Copyright Dave Clarke, 1996-2010

 

 

Visit Our Web Survey for ASP based Web Survey Software, WII Consoles (accessories, games) and World Cup Stuff (shirts, flags, accessories, kids kit)
pvr buyer  wii nintendo revolution consoles wii buyer nintendo revolution buyer