Wednesday, November 01, 2006

 

I used to be an end brace man…

Don’t worry, I’m not about to burst into song (that would be unpleasant)!

Over the past two decades, I have shed most of my personal biases towards coding style. Where once I would have argued vehemently for a particular syntax just because I currently used it, I am now always happy to be shown a better or more logical way. I still believe that any coding convention should be backed by the reason it makes the code more readable, rather than just personal preference. When there is no benefit in one style over another, then personnal preference obviously comes into play.

But the one thing I can’t shed is the placement of curly braces in the C style languages (C/C++/C#). Now I’m not trying to ignite a holy war here.

For the first 3 or 4 years I programmed in C and C++, I positioned the curly braces like this:

if () {

}


But over time and exposure to many other languages (and many other programmers’ styles) I felt that it was unbalanced so I switched my style to

if ()

{

}


where I remain. To me, the semantics suggest Begin … End, so the second style seems more natural. This seems to be the default standard for the Visual Studio IDE. I had taken heart in the fact that the majority of the code I’ve seen coming out of Microsoft generation tools has followed this style, but to my horror I recently spotted generated code that follows “1st brace at the end of line”! After a bit of nosing around it seems it’s roughly 60/40. Even the .NET Framework Design Guidelines use the first style, despite the fact that Lance Hunt’s and other popular and freely downloadable coding guidelines specify the second.

One thing I don’t understand is why developers who adhere to the second style don’t write their methods the same way? i.e.

public int MyMethod(int x, int y) {

return h = x + y;

}


So all the C# coders who read this blog, please leave a comment indicating your preferred brace style. I’m curious, so don’t be shy!


    

Powered by Blogger