Where do you put your braces?

Link. October 24, 2007. Comments [6]. Posted in: Development

If you're writing code in a programming language derived from C (i.e. one of those with pesky curly braces), where do you like putting your braces?

Some people like putting the opening brace on a line by itself right after the declaration/statement:

 NextLineBraces

I'm one of those that usually does this. It just was something I got used to from my C/C++ days and since it was the common convention in .NET back when I started fiddling around with it in 2000/2001. So this is usually what I use when coding on C#.

However, other people like to put the opening brace inline with the declaration/statement:

 InlineBraces

This question is brought to you courtesy of me noticing that IronRuby favors the "inline opening brace" style.

Actually, I use the inline-opening-brace style myself when I'm working with Java. I don't mind it, but it somehow seems wrong sometimes. I mean, using different styles when working across two languages so similar? I mean, I already have enough trouble remembering stuff like string vs. String and ToLower() vs toLowerCase() to worry about how the code looks ;-).

Historical Note: A few years back I used to be weirder and used a mixed style where I used braces-on-next-line for some things and inline-opening-brace for others depending on what it was and how many lines were in between the opening and closing braces. Yes, I was (am?) anal like that. I finally gave up on it when I grew tired of fighting the auto-formatting rules in IDEs.

What style do you favor, and why?



Wednesday, October 24, 2007 11:56:00 AM (SA Pacific Standard Time, UTC-05:00)
I tend to put my braces on the same line within control statements etc. but on a separate line when writing methods/functions etc... except on my blog where I tend to compress those down to keep posts short...

[)amien
Wednesday, October 24, 2007 12:02:13 PM (SA Pacific Standard Time, UTC-05:00)
I used to do something similar, but the VS automatic formatting just gets too much in the way :)
Wednesday, October 24, 2007 3:14:12 PM (SA Pacific Standard Time, UTC-05:00)
I like to put my braces on a new line, it gives the appearence of more readability to me, by putting a little bit of white space between your function declarations and the body of the function. Of course like all good rules, I have one exception:

Simple property getters and setters I tend to do get { return _variable; }

This probally stems from the fact that this is the default behavior of the prop snippet which I love so dearly!

josh
Josh
Wednesday, October 24, 2007 6:41:15 PM (SA Pacific Standard Time, UTC-05:00)
I put mine wherever ReSharper decides they should be. I used to have an opinion but it's too easy just to let the tool do its thing.
Friday, October 26, 2007 11:51:08 AM (SA Pacific Standard Time, UTC-05:00)
I prefer the brace on the same line style because I find that vertical space is always at a premium - I want to see more code and avoid scrolling if necessary. I'm happy that this was the default style on the DLR team when I joined.

BTW, you can always re-jig your VS 2005+ editor settings so that CTRL-A CTRL-K CTRL-F formats your code in whatever style that you want.
Wednesday, October 31, 2007 6:18:33 PM (SA Pacific Standard Time, UTC-05:00)
I like my braces on a dedicated line, it makes the code more cleaner. Sometimes when its just a single like of code, especially getter and setters, I just put them on a single line.
Comments are closed.

Syndicate

About

Tomas Restrepo is a software developer located in Colombia, South America. His interests include .NET, Connected Systems, PowerShell and lately dynamic programming languages. More...

tomasrestrepo @ twitter My Flickr photostream My saved links on delicious My Technorati Profile

email: tomas@winterdom.com
msn: tomasr@passport.com

View my profile on LinkedIn

MVP logo

Ads


Links

Categories

Statistics

Total Posts: 1032
This Year: 102
This Month: 1
This Week: 1
Comments: 801

Blogroll

Archive

Other

Copyright © 2002-2008, Tomas Restrepo.

Powered by: newtelligence dasBlog 2.1.8139.823

Sign In