notsotrivial.net

Non-trivial matters of interest to software architects and other like minded geeks.

Over the years, I've had the pleasure (and sometimes pain) of being involved in writing and maintaining the internal coding standards documents for nearly every company I've ever worked for. I still have vivid memories of many bloody internal disputes over some really silly things  - where the curly braces should go - whether or not to use underscores in front of member variables. I firmly believe you need them to keep a consistent level of code quality and to this day I will still dig in and fight for this cause. The biggest lesson I've learned:

 

"It doesn't matter what the standards are, as long as you have them and everyone agrees to adhere to them!"

 

I present to you the latest incarnations of my efforts: a zip file containing C# and VB.NET editions of my reference coding standards. I've been holding these for the holidays so that I could share a little something extra with my readers as the year winds down. I certainly don't expect everyone to agree with my standards but I hope by posting these, you can get a head start towards documenting your own.

 

Grab them. Customize them. Share with your friends.

 

Translated editions:

Portuguese by Eduardo Costa


Comments

mdosman.us

Wednesday, December 31, 2008 9:03 AM

pingback

Pingback from mdosman.us

Free C# and VB Coding Standards Reference Documents | Mohammed Osman Blog US MS IT Hyderabad India

murratore.wordpress.com

Wednesday, December 31, 2008 9:15 AM

pingback

Pingback from murratore.wordpress.com

Daily Blog Post 12/23/2008 « Murratore’s Weblog

bvsmohan in

Wednesday, December 31, 2008 9:50 AM

bvsmohan

Useful documents,
nice work

thanks

Roger us

Wednesday, December 31, 2008 10:01 AM

Roger

Excellent! My team is missing this type of document and making them adopt one is going to be a pain in itself. Discipline is hard to build from the ground up.

Jahedur Rahman

Wednesday, December 31, 2008 10:19 AM

Jahedur Rahman

Hi

Thanks for this guidelines. It would be great if you publish the files in .pdf format.
Another thing in Country "Bangladesh" is not specified. I am from Bangladesh Smile

Thanks Again

Korea Evangelists

Wednesday, December 31, 2008 10:30 AM

trackback

Trackback from Korea Evangelists

C# and VB.NET code standard document

Piseth us

Wednesday, December 31, 2008 10:38 AM

Piseth

Great, Nice tool to dig your .NET Land.

rob us

Wednesday, December 31, 2008 10:39 AM

rob

well done, thanks for sharing!

Jay us

Wednesday, December 31, 2008 11:05 AM

Jay

This is some great stuff. I can definitely use it to drive efficiency.

Nilufar in

Wednesday, December 31, 2008 11:11 AM

Nilufar

Thanks

hectorperez.wordpress.com

Wednesday, December 31, 2008 11:25 AM

pingback

Pingback from hectorperez.wordpress.com

Documentos de Nomenclatura para escribir código en C# y VB « Héctor Pérez Weblog

mtycoders.com

Wednesday, December 31, 2008 11:28 AM

pingback

Pingback from mtycoders.com

mty.Coders » Blog Archive » Documentos de Nomenclatura para escribir código en C# y VB

ebnerj.at

Wednesday, December 31, 2008 11:41 AM

pingback

Pingback from ebnerj.at

Free C# &VB Conding Standard Reference Documents at Programming with Silverlight, WPF & .NET

Satalaj More us

Wednesday, December 31, 2008 12:10 PM

Satalaj More

Thanks

US ISV Developer Evangelism Team

Wednesday, December 31, 2008 12:12 PM

trackback

Trackback from US ISV Developer Evangelism Team

Customize and Share C#, VB Coding Standards With Friends

Joe Walling us

Wednesday, December 31, 2008 12:23 PM

Joe Walling

Thanks. I am updating our company's development standards/guidelines and had missed a few of the items you included.

We agreed on most of the .NET coding standards but when we got to the database standards and use of ORM tools it got heated.

Ashraf Nadi sa

Wednesday, December 31, 2008 12:25 PM

Ashraf Nadi

Gr8 Gr8 Work Thnx

Bita ir

Wednesday, December 31, 2008 12:42 PM

Bita

Thank you a lot for sharing your valuable experience with us.
It was a long time looking for something like it.
Good luck Smile

Adem Gashi al

Wednesday, December 31, 2008 12:43 PM

Adem Gashi

Thank you very much. I appreciate your work.

Stacy Vicknair us

Wednesday, December 31, 2008 12:49 PM

Stacy Vicknair

Thanks for the reference documentation! This will come in handy and the layout is great.

keith us

Wednesday, December 31, 2008 2:10 PM

keith

Great work. Thanks

Even more important is to write the rules in Visual Studio (old FXCop) that enforce these coding standards. And to set many of these coding standards up in Visual Studio (Tools --> Options). Then as you write code - some are done automatically, others you are reminded of it.

Speednet us

Wednesday, December 31, 2008 2:13 PM

Speednet

Very nice document! I use most of your standards myself (already). It's wonderful to see your name composition standards, such as Adjective(s) + Noun + Qualifier(s) for Enums.

There is one big area I hope you'll add to your document, and I have a couple of small points.

The "big" thing is comments and documentation. I personally feel it is *essential* to document all code with the Microsoft XML-style comments before each and every object and function that is defined in the code. You can literally document almost everything, and the more you document, the better it is all-around.

For example, I have found it immensely useful to document certain Enums, placing a <summary /> comment before each value within the Enum (if their purpose is not already simple and crystal clear). Not only does that help with maintenance of the code, but it provides IntelliSense and speeds development, because you don't have to hunt down documentation of individual values.

Commenting methods/subs/functions with <param /> elements for each argument is similarly helpful. There are many more examples.

On another subject, I think the one area that VB and C# need to diverge is is naming standards for private members.

I have noticed that many people use your syntax in C# for private members that store the values for public properties. They use Pascal case for the property name and an identical name for the private member, but with camelCase. C# uses case-sensitive names, so that method works well.

In VB, however, that is not the case, and if you use the same naming standard, you are forced to use the "Me." prefix before referencing the private member. That is not ideal.

So in VB, I have found the best naming convention for the private member is to simply prepend an underscore character to the property name, and keep as Pascal case. (For example, the value for the public property "FirstName" would be stored in the private member "_FirstName".) Doing so instantly identifies that a particular name is a private member storing the value of a public property. The code becomes instantly clearer, because these types of private members are special.

One additional area I would like to see defined in greater depth is that of attributes. I personally think that attributes should always appear on their own line, one to a line. For example, your Enumeration example (6.5) should appear as follows:

<Flags()> _
Public Enum PurchaseMethod
...

That's all for now; I just wanted to add a few things that stood out in my mind, but in no way does it diminish the fantastic work you did. Thank you very much for sharing the fruits of your labor.

-Todd ("Speednet"Wink

VistaDB .NET database blog

Wednesday, December 31, 2008 2:28 PM

trackback

Trackback from VistaDB .NET database blog

Free coding standards documents (VB.NET and C#)

Vijay us

Wednesday, December 31, 2008 2:40 PM

Vijay

There will be some disagreement over any coding practice Smile
Thanks for the document. It was useful.

DotNetKicks.com

Wednesday, December 31, 2008 3:47 PM

trackback

Trackback from DotNetKicks.com

Holiday Goodie Bag: Free C# and VB Coding Standards Reference Document

Brett Mitchell us

Wednesday, December 31, 2008 4:48 PM

Brett Mitchell

Thanks for the coding standards Clint. I am in the middle of assembling the same type of document for Bisk. I have been using my own personal preferences, the book Code Complete V2 and the teams input to create a WIKI that details out our standards. And you’re right - we have had some pretty intense debates over the do’s and don’ts of various coding techniques. The underscore being a very big one. Smile

nathan prather

Wednesday, December 31, 2008 5:12 PM

nathan prather

Nice!

Thanks,
Nathan

Rachida Dukes

Wednesday, December 31, 2008 5:12 PM

Rachida Dukes

Thanks for sharing your knowledge, Happy Holidays.
Rachida

Fuji us

Wednesday, December 31, 2008 5:29 PM

Fuji

Cam on

Eduardo Costa br

Wednesday, December 31, 2008 6:12 PM

Eduardo Costa

Hi.

Good job! I have the same "role" here in my company. Is always complicated to agree about the code standards but they're really needed.

I'll share your post with my friends and co-workers.

Best regards.

Zahid Rasool

Wednesday, December 31, 2008 11:03 PM

Zahid Rasool

thanks
great document

Amgad Suliman ae

Thursday, January 01, 2009 12:13 AM

Amgad Suliman

Thank you for sharing the document. Merry Christmas.

bullpit us

Thursday, January 01, 2009 12:50 AM

bullpit

Thank you very much.

Dale us

Thursday, January 01, 2009 6:37 AM

Dale

Thanks for the documents. They're well formatted and contain some good information. But the emphasis on comments rather than on self-describing code should be changed. Comments are a bad smell in a method and usually indicate the need to extract a new method. Also, recommending foreach is probably not too good either. Microsoft confesses that foreach is 3 to 5 times slower than for.

StringBuilder performs worse than concatenation when there are less than 5 iterations or concatenations.

The exceptions portion is good but if you can comment about an exception that might be thrown then you could have tested for that exception and not thrown the exception at all - except in the case of a class library where your exception is about parameters to your own methods. Just pass the framework exception where one exists.

Ok, I know I have too much time on my hands. LOL

Vishal Khanna in

Thursday, January 01, 2009 11:42 AM

Vishal Khanna

Good work. This can be easily in Microsoft Source Analysis Tool... so that developers are get set go!

Vishal

Brad us

Thursday, January 01, 2009 12:42 PM

Brad

Thank you, i know my boss will be very happy to see this. Well written.

MJ Hufford us

Thursday, January 01, 2009 12:55 PM

MJ Hufford

Many thanks! Merry Christmas!

Talib Ali Khan sa

Friday, January 02, 2009 2:25 AM

Talib Ali Khan

Excellent work.....

Please keep up the good work...

Ario ir

Friday, January 02, 2009 2:51 AM

Ario

a million thanks...keep working ......... with the best wishes & Merry Christmas from Iran

Rohland's blog

Friday, January 02, 2009 11:26 AM

trackback

Trackback from Rohland's blog

Coding Standards (C# and VB)

Ahamed aslam in

Friday, January 02, 2009 11:24 PM

Ahamed aslam

very usefull document...thnx for sharing with us

Ali ir

Saturday, January 03, 2009 12:18 AM

Ali

Great gift. Thanks man.

What about the "with" keyword. I know it is more of best practice than coding standard, but it makes the code neater and simplifies copy-and-past if needed.

Dre767 ca

Saturday, January 03, 2009 6:19 AM

Dre767

@ Dale

You may be right, but I think it would be a completely different document if it were geared toward performance, these standards seem to be geared toward clarity.

dg us

Saturday, January 03, 2009 9:59 AM

dg

I disagree with this:

"o Write the comments first. When writing a new method, write the comments for each step the method will perform before coding a single statement. These comments will become the headings for each block of code that gets implemented."

When written well, code should be self documenting. Much of the literature on refactoring and attempts to get this message across. For example, if you find yourself writing a comment for a particular section/step of code, why not refactor that code into its own method with an intention-revealing method name?

Code documentation tends to add noise to code and cannot ever be relied on for accuracy.

AGShoro ae

Saturday, January 03, 2009 10:28 AM

AGShoro

Good workd dear thanx

Yogesh in

Saturday, January 03, 2009 11:26 AM

Yogesh

Thanks for providing good standards for coading....

pandarion.wordpress.com

Saturday, January 03, 2009 11:42 AM

pingback

Pingback from pandarion.wordpress.com

Coding Standard « Panda on Writing Tong

.NET Programmer tr

Saturday, January 03, 2009 1:03 PM

.NET Programmer

Thanks for sharing your documents.
I wish to see a code standardization document for T-SQL also Smile of course if you have one.

Great value, thanks
Eralper

PaddyLand74 in

Saturday, January 03, 2009 1:37 PM

PaddyLand74

Nice to follow these standards. Make you a better programmer.

Tosh us

Saturday, January 03, 2009 2:36 PM

Tosh

Clint,

StyleCop is a Microsoft Source Analysis tool that has coding standards that it checks. http://blogs.msdn.com/sourceanalysis/
How well do these standards match up with those of StyleCop?

Betawiz us

Saturday, January 03, 2009 2:41 PM

Betawiz

I hate to ask this question. But Who is going to enforce these standards on a team? I think automated coding standards validation with reference is always helpful. Did you try StyleCop - code.msdn.microsoft.com/.../ProjectReleases.aspx

I would suggest to help the StyleCop team to improve the StyleCop instead of creating these type of documents.

Imran Buch au

Saturday, January 03, 2009 3:00 PM

Imran Buch

Shouldn't the Constants be named using upper case letters? Please share your thoughts?

Clint Edmonson us

Saturday, January 03, 2009 4:35 PM

Clint Edmonson

Thank you to everyone for the great response to this post! Since I'm no longer working full time on a development team, these documents were collecting dust on my hard drive. They predate StyleCop and I have not verified them against the default settings in the tool.

StyleCop is definitely a good tool to add to your team's build process. Due to the general and broad nature of the standards a development team may devise, I would estimate that an automated tool could verify upwards of 80% of the rules that a team will agree upon.

freddes.se

Saturday, January 03, 2009 4:38 PM

pingback

Pingback from freddes.se

Nice coding standard documents for C# (and VB.NET) at freddes.se

pei cn

Saturday, January 03, 2009 5:08 PM

pei

It's very useful for me,
thanks a lot

marcelopetersen.com

Saturday, January 03, 2009 5:10 PM

pingback

Pingback from marcelopetersen.com

Padrões de código em C# e VB.NET | Marcelo Petersen

teoman tr

Saturday, January 03, 2009 5:18 PM

teoman

It is gonna be useful. Thanks for sharing!

Colin Wood ca

Saturday, January 03, 2009 7:05 PM

Colin Wood

Good job, needed and thanks

Manish Parmar in

Wednesday, January 07, 2009 1:02 AM

Manish Parmar

Thanks,
This work is very useful for me,
I am very thankfull to u.

dotnet-blog.com

Saturday, January 10, 2009 8:28 PM

pingback

Pingback from dotnet-blog.com

.NET BLOG | Coding Standards Reference Document

iamac0der nz

Sunday, January 11, 2009 3:06 PM

iamac0der

Great Christmas present! Thanks Clint.

It's surprising how may software-shops out there don't even have a basic coding standards reference... this is a good starting point for them.

Harry kr

Thursday, January 15, 2009 8:11 AM

Harry

Thanks a lot~!!!!

It is a useful..^^;;

Jürgen Ebner at

Saturday, January 17, 2009 12:24 AM

Jürgen Ebner

Thanks for your comment to my Blog entry.

I think it was an interessting und very useful entry so I have link your post to my link.

Cheers
Jürgen

smehrozalam.wordpress.com

Saturday, January 17, 2009 6:42 AM

pingback

Pingback from smehrozalam.wordpress.com

C# Coding Standards « Mehroz’s Experiments

fiquepordentro.net

Saturday, January 17, 2009 6:32 PM

pingback

Pingback from fiquepordentro.net

Fique por dentro Nomenclatura » Blog Archive » Holiday Goodie Bag: Free C# and VB Coding Standards Reference …

Higor Ramos br

Sunday, January 25, 2009 4:40 PM

Higor Ramos

Hello guy, I made some additions to your code standard and I'm distributing it ok? I will put your blog address. Thanks for the great document that you made.

No1 Microsoft Fan

Saturday, February 07, 2009 6:35 AM

trackback

Trackback from No1 Microsoft Fan

Eric Nelson on Free Software and how to get it

batuhanbulak.com

Tuesday, March 10, 2009 7:48 AM

pingback

Pingback from batuhanbulak.com

C# Vb.Net Coding Standart | Batuhan Bulak

Daniel Gimenez us

Saturday, March 28, 2009 8:53 PM

Daniel Gimenez

Love the doc. Wish you could provide a full example C# class demonstrating everything. Probably easier to give devs something they can reference.

J. Williams gb

Thursday, April 16, 2009 3:34 AM

J. Williams

Great style guide.
You may find the free Atomineer Utils add-in useful for the documentation comment part of your style, as it can enforce a strict style on DocXml comments, automatically builds meaningful and descriptive comments for most code elements, and even documents exceptions thrown by the method, so it takes all the pain out of keeping the documentation tidy and in sync with the actual code - It will update existing comments to reflect any changes in the code as well as creating new ones.

http://www.atomineer.com/AtomineerUtils.html

Sikis

Monday, May 25, 2009 8:01 PM

Sikis

thank you for

Mike us

Tuesday, June 16, 2009 6:48 AM

Mike

Thanks for posting this! It gives my group a great document to start from.

unidevtech.com

Wednesday, June 17, 2009 8:02 AM

pingback

Pingback from unidevtech.com

Coding Standards and SQL Mistakes | Unidev .NET Technology Blog

Dale au

Saturday, June 20, 2009 9:47 AM

Dale

Great guide.

For my employer, I'd add something about deployment standards (ie. thou shall not require Admin privileges or write into System32) and avoiding banned function calls (ie. thou shall not use function calls on the SDL banned list).

Ref: blogs.msdn.com/.../...hygiene-and-banned-apis.aspx

Sumit Joshi in

Tuesday, June 30, 2009 1:35 PM

Sumit Joshi

Great Job Done Yaaarrrr...

You really did great job. I also love to code with standards. I have studied so many standards. But what I was
following is the same as you have. You did nice work.

Live Long....yaaaaarrrrr........

Sumit Joshi
joshisumitcomp@yahoo.co.in

Sumit Joshi in

Tuesday, June 30, 2009 3:21 PM

Sumit Joshi

Great work dont yaaaarrrr...
You gave conding standard from ground and with so much clarity.
Really, I loved it. I will also share this with my company also.
I had seen so many standards but liked your documentations better.

I love to work with stardards but they should be lovable so that it
can give us pleasure...

You did the same....
Again thanks...yaaaarrr.....

Sumit Joshi
joshisumitcomp@yahoo.co.in

Dilli in

Wednesday, July 01, 2009 10:02 AM

Dilli

Thanxs dude

Comments are closed