Naming Conventions in C#

Last week I had a chance to talk to one of my friend about the naming convention they are using for their projects. He went to on to great details on how they have standards in naming convention. In my opinion, developers shouldn’t think too much about standards before writing every single line of code. The standards should be natural for development which help maintenance rather than taking developers productivity down since they have to think twice before writing code.

One thing really stick with me was the rules they use for naming convention. They have very elaborate rules on class name, method name etc.,

I like the following simple rules for naming variables;

(Names should be meaningful and easily understandable.)

Use first letter capital for for all Methods/Properties.

Use _ as the first letter for private variables. I have seen people use m_ or my as prefix for private variables.

Use first letter lower case for all method parameters.

This goes with another idea of using properties instead of private variables. There was a discussion on this topic so I am not going to go into details.I prefer to use Properties rather than private variables. By making private variables with _, it will be easy to add a FxCop rule during file check in to see if anyone violated the rule easily.

Everyone uses different rules that suits their development.

Advertisements

3 thoughts on “Naming Conventions in C#

    • Object also follows the private and public variable names I mentioned above. One thing I noticed with the variable used txtName, I will not name that field that way, I will name it with the purpose of the field like Name (do not prefix with type). Prefixing type is old school c++ model. Hope this helps.

      • Thank you for good illustration.

        But, still some developers prefer the type prefix for naming convention to get the Visual Studio’s Intelisense benefits. Also, i read this recommendation in books.

        For example, you can find txtName, for example, within a list of TextBox objects better than searching for nameTextBox or name within a list of name, Name, nameTextBox, nameLabel, namesList, and so on.

        Also, it’s easier to type a long variable name for example, strNorthwindConnectionString and dtNorthwindCustomers. It’s enough to type strN or dtN then press enter. This is better than using northwindConnectionString and distinguish it with the northwindConnection, northwindDataSet, northwindCustomersDataTable and so on.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s