Wednesday, May 19, 2010

Naming conventions, Code Style

What is a style guide?
Whenever coding in teams, it's a good idea to establish common code standards for the project that you're working on. This makes it easier to read one another's code and ensures that your code base will be standardized across the team. Although this may be a pain in the short-run, in the long-run this is a must. To further ensure code standardization in the .NET World, there's a Microsoft product out there called FXCop, but if you want it do the old fashioned way, I would recommend holding regular code reviews to make sure everyone's using proper naming conventions, and adhering to any other styles that your team/architect may decide should be used.


On my last .NET project I worked (oh .NET how I miss you so) we were using the Microsoft Standard Naming Convention. Here is the link: http://msdn.microsoft.com/en-us/library/xzf533w0(VS.71).aspx


Basically, according to the Microsoft standard, we use Camel case and Pascal case, we never use Hungarian Notation. If you need a refresher on what Camel and Pascal are, here is a link: http://msdn.microsoft.com/en-us/library/x2dbyw72(VS.71).aspx. Hungarian Notation is not in that link because Microsoft doesn’t recommend it. Hungarian Notation is when you put a small letter representing the type of object before the parameter (ex: bIsRequired or sFirstName). So, let’s not use that notation.

*UPDATE 1/23/13* I found a .net 4.0 version of the above link here: http://msdn.microsoft.com/en-us/library/vstudio/ms229045(v=vs.100).aspx

Below is a basic cheat sheet for the stuff we do regularly.

Parameter: Camel case - http://msdn.microsoft.com/en-us/library/ab6a8y1b(VS.71).aspx
Property: Pascal case and a noun phrase - http://msdn.microsoft.com/en-us/library/fzcth91k(VS.71).aspx
Method: Pascal case and a verb phrase - http://msdn.microsoft.com/en-us/library/4df752aw(VS.71).aspx
Class: Pascal case and a noun phrase - http://msdn.microsoft.com/en-us/library/4xhs4564(VS.71).aspx


There are also a couple of other things that are Microsoft Practice, but aren't in this particular guide. A buddy of mine passed these onto me saying they used to be in the hard copy version.

Class level parameters (e.g.: parameters not in a method) – underscore Camel case (ex: _isRequired, _firstName).

Constants – All caps with underscore separators (ex: IS_REQUIRED, FIRST_NAME)

Hope these references help!

No comments:

Post a Comment