Do you put the curly brace on the same or new line?

Do you put the curly brace on the same or new line?

  • Same line

    Votes: 42 40.8%
  • New line

    Votes: 61 59.2%

  • Total voters
    103

kainjow

Moderator emeritus
Original poster
Jun 15, 2000
7,745
3
Poll time! Where do you put the opening curly brace, and why?

On the same line
Code:
if (something) {
    // blah
}
Or on a new line?
Code:
if (something)
{
    // blah
}
As for me, I generally prefer it on a new line, it makes it easier to read and scan through the code. I had a professor in college who insisted it be placed on a new line. However in the "real world" I see it more on the same line.
 

dmr727

macrumors G3
Dec 29, 2007
8,621
22
NYC
I put it on a new line because it feels cleaner to me. But when reading other people's code, it's no biggie if it's on the same line. And you're right, I see it on the same line more often than not.
 

lloyddean

macrumors 65816
May 10, 2009
1,001
16
Des Moines, WA
Depends upon the employer.

And when it doesn't it depends upon what the output device is. Magazine articles and less than 19 inch displays the same line and larger displays the next line.
 

gibbz

macrumors 68030
May 31, 2007
2,691
91
I like the clean look of the new line, but it doesn't bother me to see it the other way.
 

GorillaPaws

macrumors 6502a
Oct 26, 2003
930
6
Richmond, VA
what about your else's?

Code:
if
{
    //blah
} else {
or
Code:
if
{
    //blah
}
else
{
Some always use a new line, but will combine them for else's.
 

lee1210

macrumors 68040
Jan 10, 2005
3,180
1
Dallas, TX
Boy, may as well ask vi/emacs, chocolate/vanilla, Jesus/Allah/Yewah/Vishnu/Gaia/Buddah/Zeus/Xenu/None, etc.

But I'll bite...

I put the brace on the same line. It's easier for me to read that way, grep for blocks, etc. I also like fitting more on one screen. Once a block can't be viewed at-once, I feel like it gets harder to grok what's going on. If only I could orient my displays at work to portrait.

-Lee
 

Nermal

Moderator
Staff member
Dec 7, 2002
18,678
1,183
New Zealand
Our coding standards handbook calls for braces on a new line. It also says to change any that are in the wrong place :p
 

Guiyon

macrumors 6502a
Mar 19, 2008
767
1
North Shore, MA
I usually code in either the 1TBS or Kernel Normal Form styles but I do use the Allman style where necessary (mostly to match pre-existing codebases).
 

jpyc7

macrumors 6502
Mar 8, 2009
276
0
Denver, CO
I mostly write in the so-called One True Brace Style (1TBS), a variant of K&R C according to Wikipedia.

My current employer doesn't have coding guidelines, but was acquired recently by a company that does. So I guess we will soon have them. I'm mostly writing in Python, so it doesn't really affect me.

I did join a startup company once early enough that the engineers got to hash-out the coding guidelines. Although we were writing in C and C++, the guidelines were mostly copied from Sun's Java coding guidelines.
 

Catfish_Man

macrumors 68030
Sep 13, 2001
2,579
1
Portland, OR
I've bowed to peer pressure and do same line, but my preference is new line. The point of curly braces is to delimit scopes, and by matching the column it very clearly does so.
 

mslide

macrumors 6502a
Sep 17, 2007
708
2
I do what my employer tells me to do. If it's not for work (e.g. open source), I'll match the style of whatever is used, if the style's not already specified somewhere. Other than that, it doesn't really matter although for my personal projects I tend to put the brace on the same line so I can fit more code on the screen at once.

All that really matters with stuff like this is that it is readable and consistent through out the project.
 

notjustjay

macrumors 603
Sep 19, 2003
6,040
111
Canada, eh?
I think today with high resolution monitors, conserving whitespace is less of an issue. I like the new line approach because it looks clean, makes it easy to delineate exactly where a block begins and ends (just scan up or down the page looking for the brace in the same column), and also makes it easy to move code around (just cut/paste at the braces).

And I like emacs, thank you very much (though I use SlickEdit most of the time).
 

whooleytoo

macrumors 604
Aug 2, 2002
6,559
628
Cork, Ireland.
I always put it on a new line; for me it more clearly defines the scope of the block of code. And clarity of code is very, very important. If you need to see more code on screen, buy your developers larger screens. It'll pay back in the long run.

We don't have a coding standard here, but when one developer here was maintaining code I developed, he systematically went through it and changed them all - which was a bit annoying. Now I'm maintaining it again, I change them as/when code requires changes.
 

Transporteur

macrumors 68030
Nov 30, 2008
2,729
3
UK
During the actual coding process I do prefer the new line method because it makes the code easier to read, especially when you're dealing with dozens of nesting levels.

But while giving the code its last polish I generally move the bracket to the upper line.
 

Sydde

macrumors 68020
Aug 17, 2009
2,104
2,162
IOKWARDI
Do you ever write a short method on a single line, braces and all? Like when you had to write out all the setter/accessor methods?
 

mdatwood

macrumors 6502a
Mar 14, 2010
684
212
East Coast, USA
To the original question, same line...always.

Do you ever write a short method on a single line, braces and all? Like when you had to write out all the setter/accessor methods?
Yes, all the time especially when doing properties in c# for example. I hate having this huge block of code with only 1-2 lines that actually do anything. Much easier to scan code by keeping it on the screen.
 

BadWolf13

macrumors 6502
Dec 17, 2009
267
0
With if statements, on the same line, with methods, it's a new line. I just think they both look neater that way.
 

notjustjay

macrumors 603
Sep 19, 2003
6,040
111
Canada, eh?
Do you ever write a short method on a single line, braces and all? Like when you had to write out all the setter/accessor methods?
Yes. Any rule has exceptions, and if it makes the code easier to read and maintain, then it's justified.

For example, our coding standard discourages the use of the ternary operator, but it has its uses.

We don't have a coding standard here, but when one developer here was maintaining code I developed, he systematically went through it and changed them all - which was a bit annoying. Now I'm maintaining it again, I change them as/when code requires changes.
Many editors have a "beautify" or code formatting command which lets you choose the options you prefer, and apply them to the entire file once and for all.
 

chrono1081

macrumors 604
Jan 26, 2008
7,432
1,401
Isla Nublar
I put it on a new line because it feels cleaner to me. But when reading other people's code, it's no biggie if it's on the same line. And you're right, I see it on the same line more often than not.
+1

I prefer it on a new line but there are people who fuss about that if they read my code.

I'm really picky and love to have sexy looking code. I don't mind typing more to make my code look cleaner.
 

Sydde

macrumors 68020
Aug 17, 2009
2,104
2,162
IOKWARDI
I guess I have been spoiled by the XCode editor, with is handy code folding and range highlighter. To me, the same line always seems more neat.