View Poll Results: Where do you put your opening brace?

Voters
53. You may not vote on this poll
  • Style 1 (opening brace on same line)

    18 33.96%
  • Style 2 (opening brace on next line)

    35 66.04%

Thread: Curly Brace Placement

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912

    Curly Brace Placement

    I was almost certain we had already had this poll, but all the searching in the world couldn't seem to find it. So here it is: curly brace placement has been a hotly debated topic at my work place. Arguments have included the popularity of one style over the other, companies that use it in their documentation, etc... What do you use? Any particular reason why?

    Style 1:
    Code:
    keyword {
    
    }
    Style 2:
    Code:
    keyword
    {
    
    }
    I used to use style 2, because I learned C from Borland manuals and that's what they use. However I switched to style 1 at my current job because it's what my first project was already using - and now it's just a habit.

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    I use 1. because I used to work on terminals where I only had 24 lines of visible space in the editor, and a brace by itself wastes a line. I consider

    Code:
    if (something) {
         // do something
    } else {
        // don't do something
    }
    vastly superior to

    Code:
    if (something)
    {
         // do something
    } 
    else 
    {
        // don't do something
    }
    Five lines vs. eight.

    Instead of having the debate in the workplace, however, just use a source code formatter so everyone can see what they like. Such debates in the workplace have been raging for decades and are a waste of time.
    Last edited by medievalelks; 05-22-2009 at 08:23 AM.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    #2 all the way, #1 looks unbalanced and is harder to read (looks messy).
    ...and in the end these questions are just a matter of opinion.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    I use both:

    #2 for functions
    #1 for anything else

    Citing from /usr/src/linux/Documentation/CodingStyle:

    Heretic people all over the world have claimed that this inconsistency
    is ... well ... inconsistent, but all right-thinking people know that
    (a) K&R are _right_ and (b) K&R are right. Besides, functions are
    special anyway (you can't nest them in C).
    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Neither. I have resolved not to use opening braces!

    Just kidding. Digraph and trigraph sequences are more verbose anyway
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Style 3:
    Code:
    Keyword 
            {
          
            }
    *vomits*

    #1 ftw
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  7. #7
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Code:
     
    if(true){
       dostuff();
       } else {
       dootherstuff();
       }

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Line breaks are for weenies. So are spaces and tabs. I mush everything together all on one line.
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Line breaks are for weenies. So are spaces and tabs. I mush everything together all on one line.
    If I had posted this on the other forum I frequent, I would've expected the response, "Neither - real programmers only use parentheses".

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sean
    If I had posted this on the other forum I frequent, I would've expected the response, "Neither - real programmers only use parentheses".
    Well, I suppose I came close, e.g.,
    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    <%
        if (argc > 1)
        <%
            printf("%s\n", argv[1]);
        %>
        return 0;
    %>
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Code:
    #include <stdio.h>
    
    extern int user_chose(voter_t voter, int option);
    extern voter_t get_voter(void);
    
    int main(void)
    {
        voter_t voter = get_voter();
    
        if (user_chose(voter, 1)) {
            printf("Totally agree\n");
            return 0;
        } else {
            printf("I don't agree\n");
            return 1;
        }
    }
    @laserlight: Free to edit it.

  12. #12
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    I've always used style #2. I feel style #1 is quite messy. IMO, with proper indentation, style #2 is easier to read compared to style #1.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I personally use style #2 because I find it easier to follow. At work, it's brace on it's own line, but braces are indented to the level of the code inside the brace, rather than the level of the enclosing code - I don't like it much, but that's life.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    i use style number 2! because i learned C++ ( and still learning ) from Deitels C++ how to program.
    its really more readable considered to the first style!
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  15. #15
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    I like them both equally, but find that when I write code I use style #1 if that makes sense.
    Woop?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. is curly braces here is a must?
    By mashour06 in forum C Programming
    Replies: 8
    Last Post: 04-25-2009, 04:41 PM
  2. Curly brace question
    By kenryuakuma in forum C++ Programming
    Replies: 12
    Last Post: 12-16-2008, 07:06 PM
  3. Placement new?
    By Elysia in forum C++ Programming
    Replies: 9
    Last Post: 02-26-2008, 04:50 AM
  4. My Placement come true
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 06-07-2004, 06:07 AM
  5. CIOS college placement tests
    By compjinx in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 12-13-2002, 02:33 AM

Tags for this Thread