代码改变世界

Use proper Naming Conventions

2011-03-05 10:44  杨延成  阅读(434)  评论(0编辑  收藏  举报
You should prefer proper naming conventions for consistency of your code. It is very easy to maintain the code if used consistent naming all over the solution. Here are some naming conventions which generally followed by .Net developers:

* Always use Camel case (A word with the first letter lowercase, and the first letter of each subsequent word-part capitalized) while declaring variables.
* Use Pascal (A word with the first letter capitalized, and the first letter of each subsequent word-part capitalized) naming format while declaring Properties.
* Avoid all uppercase or lowercase names for properties, variables or method names. Use all uppercase when declaring const variables.
* Never use a name begins with numeric character.
* Always prefer meaningful name for your class, property, method etc. This will be very useful for you to maintain the code in future. For example, “P” will not give proper meaning for a class. You will find it difficult to know about the class. But if you use “Person”, you will easily understand by it.
* Never build different name varied by capitalization. It is a very bad practice. It will not be useful while developing code, as you will not know what is “person” class and what is “Person” class!!! But from the above scenario, it can be very easily understandable that “person” is an instance variable of “Person” class.
* Don’t use the same name used in .Net Framework. People who are new to your code have a very difficulty to understand it easily.
* Avoid adding prefixes or suffixes for your identifiers. Though in some guidelines they use “m_” and in some other they use “_” as the prefix of variable declaration. I think it is not that much useful. But, it depends on your organizational coding practices. This point is contradictory based on various organization and there is no strict guidance on it.
* Always use “I” as prefix for Interfaces. This is a common practice for declaring interfaces.
* Always add “Exception” as suffix for your custom exception class. It will give better visibility to your exception class.
* Never prefix or suffix the class name to it’s property names. It will unnecessary increase the property name. If “Firstname” is a property of “Person” class, you can easily identify it from that class directly. No need to write “PersonFirstname” or “FirstnameOfPerson”.
* Prefix “Is”, “Has” or “Can” for boolean properties like “IsVisible”, “HasChildren”, “CanExecute”. These gives proper meaning to the properties.
* Don’t add prefix for your controls, instead write proper name to identify the control.