I’m a firm supporter of coding conventions (at least of my coding conventions).
Software factories [^] [^] [^] and other code generation tools have been taking care of writing the tedious (and, sometimes, ugly) code but, at some point, some code must be written and read by human developers. That’s why (in my opinion) the way the code is written is as much important as the language it is written in.
There are several resources about coding conventions for the .NET framework:
I haven’t read Brad and Krzysztof‘s book (yet), but I’ve read MSDN Library‘s guidelines and Juval‘s document and watched Krzysztof‘s presentation.
I have to disagree with Microsoft’s position about naming conventions of private members. In an enterprise as big as Microsoft projects start and end, people move between teams and every time someone changes team he/she is faced with the possibility of having to learn and adapt to a new set of naming conventions for private members.
IDesign goes one step further on defining naming conventions for private members. I just don’t like the convention they used. What’s the point of prefixing private member fields (not variables as in their document) with m_? What does it mean? Member? If it’s not a local method variable or a method parameter it’s a member.
Some just prefix with _. Once again, why? What does that mean?
The only reason I can find for this prefixing practice is lost in the old days of colorless IDEs, printers and books. With colorful IDEs like Visual Studio 2005 you just need to qualify instance member fields with the this (Me for Visual Basic) keyword and static (Shared in Visual Basic) member fields with (lacking of a better solution) the class name.
Lets pretend for a moment that I’m one of the prefixes. How would I do it?
- i_camelCase for private instance members.
- s_camelCase for private static members.
- c_camelCase for private constants.
Why stop there? Let’s also prefix method variables:
- v_camelCase for method variables.
Now I’m on a roll. Let’s prefix method parameters:
- i_camelCase for input parameters.
- o_camelCase for output parameters.
- r_camelCase for by reference parameters.
The next step would be to go Hungarian and add type information to the names, right? No. Let’s stop here and get in the right track.
My Naming Conventions for C#
I Find that keeping naming conventions as simple as possible is the best way use and validate them.
- Pascal casing for any class/struct member (constants, fields, properties, methods, events) except for private fields.
- Camel casing for private fields and method/property parameters and variables.
- Prefix interface names with I (IAccountService).
- Prefix type parameter names with T (TKey, TValue).
- Suffix type with meaningful names for its purpose (AccountService, InvalidAccountException, MustVerifyAccountAttribute, FromAccountTextBox).
- Method names must reflect what they are supposed to do. Methods always do something, so they must start with a verb. (GetAccountInformation, UploadFile)
- Names must be descriptive. Abbreviations should not be used. Hungarian notation should not be used. Single name variables should not be used.
- Language keywords should be used instead of type names (string instead of System.String, int instead of System.Int32, etc.).
- Business relatad names should reflect business entities or actions, and in the business’ language (GetContaFromDataBase, ContaInválidaException, ValidarContaAttribute).
- Underscores should not be used. Camel casing should be used to separate words.
- Acronyms should be treated as a single word. Just to be compliant with Microsoft, for acronyms with less than three letters, all letters should be upper case.