Saturday, January 2, 2010

Oops, I've Been Breaking Many .NET Coding Standards!

Back in the classic VB3-6/VBA days, I heavily used hungarian notation as it was the naming convention standard. Assumed I should continue following the same standard when naming my objects in .NET. Found out this is wrong and that I've been violating several .NET / C# best practices.

I happened across Steve Orr's website which pointed me to several useful MSDN articles.

Some great takeaways regarding what I've been doing wrong include:
1) Naming Conventions, General:
a) Do not use underscores, hyphens, etc.
b) Use CLR type names, not language-specific names...that means use Int16, not C#'s short
c) Do not prefix with "C" for class or "O" for object like used to in classic VB
2) Naming Conventions, Upper Case:
a) Do not use Hungarian notation
b) All objects are pascal-case, except parameters which are camel-case
c) Two letter acronyms are always uppercase unless they are at the beginning of a parameter name, then they match and all stay lowercase
d) Three+ letter acronyms are treated like words (first letter only upper case)
3) Namespaces
a) Avoid too many or too deep namespaces
b) Separate advanced namespaces from common namespace by suffixing with "xxx.Advanced"
4) Static Classes:*2
a) Use static classes sparingly, they are not a miscellaneous bucket
5) Property Design
a) Return original value if error encountered
6) Parameter Design:
a) Always place out parameters to the right after byval and ref parms...even if inconsistent overloading order

Pascal-Case:
First letter of every word is uppercase (e.g.: SampleObjectName)


Camel-Case:
Same as pascal case, except first letter always lower case (e.g.: sampleObjectName)

No comments:

Post a Comment