C# Source Code Formatting Preferences

I just tried checking in some C# code and ran into a merge error so I fired up $g(Araxis Merge) and noticed hundreds of unexpected changes all to whitespace! That was a bummer because I just wanted to check in and call it a night but alas, it’ll have to wait a day. Now, I know code formatting can be a religious issue but…

Here are a few examples, the actual code of which is unimportant for the purposes of this discussion:

1 public void foo(string locator) { 2 try { 3 if (selenium.IsElementPresent(locator)) 4 break; 5 } catch (Exception) { 6 } 7 }


1 public void foo(string locator) 2 { 3 try 4 { 5 if (selenium.IsElementPresent(locator)) 6 break; 7 } 8 catch (Exception) 9 { } 10 }

I’d call the latter more “mainstream” as the majority of Open Source projects I’ve looks at (including dasBlog, FileHelpers, RssToolkit, SubSonic, CCNET and
all seem to align the braces with a few exceptions like the empty catch block or a single statement block. I haven’t downloaded the .NET sources yet but I’d be interested to know what Microsoft uses.

Chuck Jazdzewski, a well known ‘softie publishes a good deal of C# code on his blog formatted using the top style. A few colleagues, John Waters, Falafel‘s CTO and Adam Andersen have both blogged using the latter style, here and here respectively.

Which do you prefer? Do you consider either more “mainstream” than the other?

[Update: April 15, 2008] Added a list of OS projects I’ve looked at.

11 thoughts on “C# Source Code Formatting Preferences

  1. The first style is the Java community style, the second the C# community style – though the exception block throwing away exceptions is never recommended enough to have a standard representation of ‘{ }’ 🙂
    C and C++ styles exist that use either, and are probably the sources for the different community styles. MS source generally uses the latter style.
    I prefer using the style appropriate to the community in question. When coding C, JavaScript and other C-style languages, I use the C# style. However, I don’t have a problem with Java-style code. It just reminds me it’s written in Java.

  2. I prefer the latter style. The additional white space is more visually appealing to me. When I first look at the first style, the first thing that registers in my mind is "selenium.exception". I then see the entire block, but it’s just a momentary disconnect for me. With the latter block, I comprehended it instantly.
    I’m also coming from a Delphi background and the latter style is more in line with the coding standards that I follow with that language.

  3. I strongly prefer the latter style. When I was doing some Java work, the formatting of braces drove me nuts, until I simply decided to ignore the community.
    I’d favor a consistent style, in general, consistent with community preferences, but the Java style is, to my notion, counterproductive. The great deficiency of C-style languages is their use of braces, which are easily overlooked on the page, rather than more highly visible tokens, such as begin and end. Braces do not survive multi-generation xeroxing well, and even on a laser, if you use the economy setting for toner density, can be troublesome.
    What I am still wishing for, however, is a C# equivalent to DelForEx, which has been a mainstay of my coding for years now. A good formatter is critical, in my experience, when merging code from other authors. My own style becomes second nature, transparent as I read it, but mixing in different styles very definitely inhibits readability.

  4. I am definitely in the minority at Microsoft. Most use the latter formatting style and it is also the default formatting options for VS.
    I use a modification of the former style because I find it easier to read and it is much more consise for blog posts than keeping the braces aligned.

  5. I used to prefer and use the first style, but once I found out I was in the minority, I switched to the latter style (although I’d have the braces for the catch block on seperate lines. Perhaps this ‘following the sheep’ attitude has something to do with being a New Zealander 🙂

  6. Thanks guys for your comments!
    Hey Chuck,
    Hope all is well! I’d be interested to know if there are any internal requirements at MS to follow adhere to a particular style? How to developers on your team deal with multiple formatting styles or have you all agreed on one style?

  7. I use the latter by default since that’s how Visual Studio formats your code out of the box. While I often find the former a little more readable I find consistancy of code formatting to be the real readability booster. I’ve rarely found a good reason to deviant from the standard and since I work in teams a lot I try to think more in terms of the good of the whole vs. what my grump self my prefer.
    I remember using the Java and C# style of code formatting in my Delphi applications and that was a bloody mess. I completely confused new people looking at my old code and when I’d post samples online or questions I had to either reformat my code or explain my own mutant style. In the end I looked at the included VCL source and formatted everything just like that.
    Funny enough I just fixed a bug in SubSonic where it always assumed there was a trailing curly brace when compiling migrations.

  8. @Bill Meyer – To me it seems Visual Studio 2008’s "Format Document" is pretty close to DelForEx. You have pretty tight control over code formatting in the options plus the added benifit of it formatting your code for you as you type vs. having to go back through. I’m curious what features are missing for your needs?

  9. I normally use the former style. With the one exception that I NEVER use if statements without blocks. Way to easy to forget adding the block when you add one more line to the statment.
    While I haven’t got problems with the later style, I don’t like using it because all those 1-line if statments (remember: For me always with brackets…) will then suddently look very strange.

  10. Like most, I use the latter style. I did however get marked down in a computer science Java class for using the latter style. I used Visual Studio and J# to do my assignment rather than Eclipse and didn’t change the braces. I tried to explain the style difference to the TA but he was having none of it.
    I would be interested to see in Studio 2008 with JavaScript support, how Microsoft handles braces there. Is there an easy way to format the .js file so it has no whitespace to make it more efficient on the web?

Comments are closed.