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. | |
| Method | Pascal | GetDetails, UpdateStore | Methods should use verbs or verb phrases. | |
| Parameter | camel | personName, bankCode | ||
| Interface | Pascal with "I" prefix | IDisposable | One of the few times when prefixing is adopted. | |
| 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. | |
| 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. |
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.YourProjectGeneric reusable routines, classes etc which will be used across projects, can sit at the 'company' or 'dept' level, for example:
Visualize.Blog
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:///Full details of more XML tags can be found in the Visual Studio.Net on-line help.
/// SendSMSMessage sends a text message to a valid mobile number
///
/// Mobile number to send to in format +44 1234 112233
/// Message itself, max 160 chars
///Message result string from gateway
public string SendSMSMessage(string mobileNo,string message)
{
// rest of code here...
No comments:
Post a Comment