.Net Coding Standards
Written by – Vinay Hatwal
28-Jul-2009
Code, anybody can write. By having only few months of programming study or experience, anybody can write a “working application”, but writing a good code is like a passion. By writing a working code you can develop an application but as soon as it will become larger, even you will not be able to understand it. Even when u will want to add some new functionality or you will want to modify your application, you will face a lot of problems.
So do not just write Working Applications, always write Good Code for your application so that u can make it programmer friendly.
Writing a good code should be included in your good habits, in your hobbies, so that your application should be highly manageable. For this purpose you have to follow some standards regarding writing code for you application not only in .Net but also in other programming languages.
After a lot of study and a lot of analysis on the Coding Standards, I am summarizing these coding standards in my article.
1) Naming Conventions
| Object | Notation | Good | Bad |
| Class,
Structure, Method, Property Enumeration, Namespaces, Delegates
|
Pascal Casing | EmployeeMaster,
StudentMaster |
employeeMaster, EMPLOYEEMASTER,
employee_master |
| Interface | Pascal Casing but start with I | IUnknown | iUnknown ,
iunknown, IUNKNOWN |
| Variables
Objects Method Parameters |
Camel Casing | userName
userId userAge |
Userid
User_Id USERID UserId |
| Class level variables | Camel Casing starting with a underscore | _userName
_userId |
Userid
User_Id USERID UserId userId |
| Constants | All Caps with underscore | CONS_SUPERUSER | Cons_superuser
consSuperuser |
2) Variable Naming Conventions
- Do not use Hungarian Notation with the variable names because they will become more complex to understand with it. Like –
Dim m_strRefUserName as String
It shows module level string variable with reference for holding Username. Now think lot of variable declaration in your project. Don’t you think these will create a lot of confusion?
- Do not use underscore (_) with local variables.
- Do not use single character variable name like I, j, k. Instead use meaning full variable names like Index, Counter.
- Do not use abbreviation with the variable names. Like instead of writing
Dim add as string
Use
Dim address as string
- Never use reserved keywords for variable names.
3) Good Programming Practice
- Do not hardcode numbers. Instead use XML Serialization Classes to provide the common setting for your Software. So that you would be able to change these values without making changes in the code.
- Never hardcode the Strings, Images and Icons. Always use resource file.
- Always use is as prefixing Boolean return type methods –
Private Function IsListEmpty() as Boolean
End Sub
- Use String.Empty() instead of using “” for the empty string.
- Try to use less class level variables. Instead use local variable. Because you can’t be sure which method is responsible for changing its value.
- Do not use String for Discrete Values. Instead use Enums.
-
- Never hardcode a path or drive name in code. Get the application path programmatically and use relative path.
- Error messages should help the user to solve the problem. Never give error messages like “Error in Application”, “There is an error” etc. Instead give specific messages like “Failed to update database. Please make sure the login id and password are correct.”
- Do not have more than one class in a single file.
- Use the AssemblyInfo file to fill information like version number, description, company name, copyright notice etc.
- Logically organize all your files within appropriate folders. Use 2 level folder hierarchies. You can have up to 10 folders in the root folder and each folder can have up to 5 sub folders. If you have too many folders than cannot be accommodated with the above mentioned 2 level hierarchy, you may need re factoring into multiple assemblies
- If you are opening database connections, sockets, file stream etc, always close them in the finally block. This will ensure that even if an exception occurs after opening the connection, it will be safely closed in the finally block
- Always create the Log for your application. So that you can easily investigate where is the problem in your application. Even u can log for many of the information like
- Error Messages
- Incomming Data
- Outgoing Data
- Warning Messages
- Results
- Normal Messages
- DateTime
- Etc
- Always mark your name, date of creation, and main use at the top of the module. And make a revision history with the same information whenever the changes are required.
- Always write default values of the Setting with the constants.
- Do not code like a structure programming. Always use OOPs with .net.
4) Architecture
- Always use multi layer (N-Tier) architecture.
- Never access database from the UI pages. Always have a data layer class which performs all the database related tasks. This will help you support or migrate to another database back end easily.
- Separate your application into multiple assemblies. Group all independent utility classes into a separate class library. All your database related files can be in another class library.
5) Comments
- Do not write comments for every line of code and every variable declared.
- Write comments wherever required. But good readable code will require very less comments. If all variables and method names are meaningful, that would make the code very readable and will not need many comments
- Always write XML Comments to describe the functionality of a class and Methods. They will help you and others to understand the functionality of the Method at the time you r using them.
Resources

