C# Coding Standards

Due to a peculiar set of circumstances I’m looking at C# coding standards or style guides. The key decision is to agree to a style guide that everyone more or less follows. I also like style guides that go a bit beyond how to format the curly braces on a newline or tabs versus spaces. I like rules like this:

Define small classes and small methods.

— Rule #69 The Elements of Java Style

At a previous organization:

  1. I bought a bunch of copies of The Elements of Java Style.
  2. I handed them out to all of our developers.
  3. I ran a brown bag lunch covering the top 10 rules I wanted them to concentrate on.
  4. We checked most of the style items in the continuous build with Checkstyle. Gentle enforcement.

A quick perusal of the C# world says there isn’t any base coding standard like Sun’s Coding Conventions. I’m a bit surprised since I expected Microsoft to have at least attempted to do so, but other than the things you can glean from heaps of example code it doesn’t appear they have a well documented default.

The candidates from fifteen minutes of googling appear to be:

The ic#code standard is concise and covers the basics. The iDesign option is more comprehensive. A quick skim did found a rule I don’t agree with:

Avoid methods with more than 200 lines.

Two hundred lines is ripe for refactoring to many 10-20 line methods. Still it is a lot more in depth. The final Philips standard attempts to combine rules from their C++ Coding Standard, the ECMA C# language spec, and an MSDN Design Guidelines Library Development document.

I’m pretty open at this point to suggestions from any of you who have spent a lot of time in the C# trenches.

9 responses to “C# Coding Standards”

  1. Rob Gibbens says:

    Actually, there is a comprehensive set of standards, called the “Framework Design Guidelines”. The book was just released in it’s second edition (485 pages I believe). The rules defined there are also the rules codified in the static analysis tool inside Visual Studio. They are available online also…just search for “.net framework design guidelines”.

  2. brad says:

    If you are looking around for an arbitrary set of rules to follow, you are focusing on the wrong thing and are setting your self up for a series of pointless rule-based victories on the road to failure.

  3. Damon Zirkler says:

    We based our style guide on Lance Hunt’s C# style guide, found here: http://weblogs.asp.net/lhunt/pages/CSharp-Coding-Standards-document.aspx

  4. Ed Gibbs says:

    Thanks for the suggestions. And a little extra background, I’ve been asked to help with an effort in another department to switch their staff to .NET. A coding standard is just a small step in setting them off on the right path. I’d rather we didn’t add another technology to our stack, but apparently that train had already left the station.

  5. Mark Levison says:

    As has already been said the book:
    Framework Design Guidelines is a good start. Also try FxCop which has a bunch of rules already builtin. Frankly you will have to spend some time turning some of them off.

  6. Ed Gibbs says:

    Given this is another internal group who are just coming up to speed, I think the Framework Design Guidelines are a little heavy for them. They’re unlikely to have the context to decide rules that are meant for APIs versus things that apply generally. For instance the amount of comments for documentation.

  7. John Doe says:

    Ed: “the Framework Design Guidelines are a little heavy” Maybe, but the .NET Framework is complex, too.

    However, the examples you give: “Define small classes and small methods” and “Avoid methods with more than 200 lines”, they have nothing to do with C# or any other specific language, its general coding style. Therefore, I would prefer to recommend “Code Complete”.

  8. Ed Gibbs says:

    General coding style things tend to be some of the most useful parts of coding standards. Unfortunately they’re probably not followed a lot. Code Complete isn’t a bad idea to suggest to the other internal group.

  9. […] via C# Coding Standards | Musings of a Software Development Manager. […]