I just tried checking in some C# code and ran into a merge error so I fired up
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 }
and
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
SubText) 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.