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

Discussion in 'Mac Programming' started by kainjow, Apr 11, 2010.

?

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

  1. Same line

    42 vote(s)
    40.8%
  2. New line

    61 vote(s)
    59.2%
  1. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #1
    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.
     
  2. dmr727 macrumors G3

    dmr727

    Joined:
    Dec 29, 2007
    Location:
    Southern California
    #2
    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.
     
  3. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #3
    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.
     
  4. gibbz macrumors 68030

    Joined:
    May 31, 2007
    #4
    I like the clean look of the new line, but it doesn't bother me to see it the other way.
     
  5. -aggie- macrumors P6

    -aggie-

    Joined:
    Jun 19, 2009
    Location:
    Where bunnies are welcome.
    #5
    A lot of texts show it on the first line, but I like it on the second.
     
  6. GorillaPaws macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #6
    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.
     
  7. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #7
    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
     
  8. Nermal Moderator

    Nermal

    Staff Member

    Joined:
    Dec 7, 2002
    Location:
    New Zealand
    #8
    Our coding standards handbook calls for braces on a new line. It also says to change any that are in the wrong place :p
     
  9. Guiyon macrumors 6502a

    Joined:
    Mar 19, 2008
    Location:
    North Shore, MA
    #9
    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).
     
  10. dmr727 macrumors G3

    dmr727

    Joined:
    Dec 29, 2007
    Location:
    Southern California
    #10
    Wait, that's not even a question. emacs? emacs? Do you want some apple juice while you write code? Maybe a pacifier? Even my grandmother thinks emacs is for sissies. :p
     
  11. jpyc7 macrumors 6502

    Joined:
    Mar 8, 2009
    Location:
    Denver, CO
    #11
    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.
     
  12. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #12
    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.
     
  13. mslide macrumors 6502a

    Joined:
    Sep 17, 2007
    #13
    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.
     
  14. notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #14
    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).
     
  15. whooleytoo macrumors 603

    whooleytoo

    Joined:
    Aug 2, 2002
    Location:
    Cork, Ireland.
    #15
    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.
     
  16. Transporteur macrumors 68030

    Joined:
    Nov 30, 2008
    Location:
    UK
    #16
    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.
     
  17. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #17
    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?
     
  18. mags631 Guest

    Joined:
    Mar 6, 2007
    #18
    None of the above. I've been coding in Python of late.
     
  19. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #19
    I put my braces in my mouth.

    Couldn't resist.
     
  20. mdatwood macrumors 6502a

    Joined:
    Mar 14, 2010
    Location:
    Denver, CO
    #20
    To the original question, same line...always.

    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.
     
  21. BadWolf13 macrumors 6502

    Joined:
    Dec 17, 2009
    #21
    With if statements, on the same line, with methods, it's a new line. I just think they both look neater that way.
     
  22. Detrius macrumors 68000

    Joined:
    Sep 10, 2008
    Location:
    Asheville, NC
    #22
    This.

    When the open brace is at the end of a line, it's not abundantly clear where the matching close brace is.
     
  23. notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #23
    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.

    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.
     
  24. chrono1081 macrumors 604

    chrono1081

    Joined:
    Jan 26, 2008
    Location:
    Isla Nublar
    #24
    +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.
     
  25. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #25
    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.
     

Share This Page