Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

POLL : Braces On / Under

Options
  • 16-06-2012 4:53pm
    #1
    Registered Users Posts: 891 ✭✭✭


    Ok,

    So there are a few language that are whitespaceless and use some form of brace to identify a code block or method, such as C++, C#, Java etc...

    By braces, I mean curly brackets

    Some people (myself included) use this (on);
    [INDENT]void methodName()[B]{[/B][INDENT]// CodeBlock[/INDENT]
    [B]}[/B][/INDENT]
    

    Others use this style (under);
    [INDENT]void methodName()
    [B]{[/B][INDENT]// CodeBlock[/INDENT]
    [B]}[/B][/INDENT]
    

    If I'm completely honest, the latter freaks me the ***k out. This may even sound strange to some as the only difference between the two is a carriage return line feed. The former is the standard generated by most of the IDE's I've come across, which makes me think it *is* the standard.

    As whitespace isn't important, the question is, which style do you use, and if its the *under* style above....for the love of god WHY??!?

    Do you have your braces under or on? 34 votes

    Braces on the same line! (On)
    0%
    Braces under! (Under)
    44%
    mewsojebuzSkrynesaverMmmm_LemonyNiall09IRLConorhenryporterUnregistered.ClanketciaranmacFluffy88Jayo_MKavrocksBallyv24dalta5billion 15 votes
    Compile Error: braces.poll: Line 2: Don't trust a man who wears braces and a belt (Meh!0
    55%
    ManachPeter BdazberryThe_B_ManMaliceDonkeyStyle \o/fergalrfl4pj4ckGenghiz CohenCreepingDeathYearning4StormyUseful.Idiotconor.hogan.2aperture_nuigMo14AswertyRaging_NinjaBored AccountantEncrypted Pigeon 19 votes


Comments

  • Registered Users Posts: 2,345 ✭✭✭Kavrocks


    Braces under! (Under)
    I don't like the look of braces underneath it just doesn't look right to me.


  • Closed Accounts Posts: 7,145 ✭✭✭DonkeyStyle \o/


    Compile Error: braces.poll: Line 2: Don't trust a man who wears braces and a belt (Meh!0
    The first C++ book I read used under braces, and I don't think my first IDE had the facility to match braces. So having them really visible and lined up with the indentation made perfect sense. Now it's just what looks right to me.


  • Registered Users Posts: 891 ✭✭✭Mmmm_Lemony


    Braces under! (Under)
    From what information I gather, its attributed to 'The C Programming Language' book.

    I can completely understand that if its what you have been taught, it's a hard habbit to change.

    Personally I feel the *on* method is clearer. Didn't realise there were actual categories until today. It makes a huge difference when I am working on someone elses code. I can get very frustrated trying to work on code when its not in the *style* I am accustomed to. (e.g. braces and or general indentation, incorrect camelCase etc..)

    What are you experiences with this DonkeyStyle?


  • Registered Users Posts: 2,024 ✭✭✭Colonel Panic


    I follow the style of whatever code I'm working on, but for my personal stuff, I put the brace on a new line, use PascalCase for functions and methods and camelCase for variables and 3 spaces as a tab.

    Just use whatever you like as long as it's consistent.


  • Registered Users Posts: 1,717 ✭✭✭Raging_Ninja


    Compile Error: braces.poll: Line 2: Don't trust a man who wears braces and a belt (Meh!0
    I'm not a great fan of the egyptian style (same line). When the curly brackets are on different lines it helps me to see the code better, and to more easily see nested loops and distinguish between methods. Otherwise I feel the code becomes a block of text, even with indentation.


  • Advertisement
  • Closed Accounts Posts: 7,145 ✭✭✭DonkeyStyle \o/


    Compile Error: braces.poll: Line 2: Don't trust a man who wears braces and a belt (Meh!0
    I can get very frustrated trying to work on code when its not in the *style* I am accustomed to. (e.g. braces and or general indentation, incorrect camelCase etc..)

    What are you experiences with this DonkeyStyle?
    Well I'm a PHP guy mostly, so I try to stick to the Zend coding style, which uses a mixture of both depending on what you're doing, so you can end up with:
    [php]
    function blah()
    {
    if(x) {
    //
    } else {
    //
    }
    }
    [/php]


  • Registered Users Posts: 5,246 ✭✭✭conor.hogan.2


    Compile Error: braces.poll: Line 2: Don't trust a man who wears braces and a belt (Meh!0
    Under the line personally, but whatever is the convention I have no problem using once it is 100% consistent within the project/file/whatever.

    This probably is because I learned C first and mostly ANSI C so it is K&R + ANSI so full on newline for all braces. But whatever exists before I adopt.

    Variable naming conventions changes depending on language ie. Ruby is much different to Java.


  • Registered Users Posts: 11,980 ✭✭✭✭Giblet


    It's probably the least important, but easiest barrier of entry argument there is wrt programming.
    (PS: Underline because you define the code block symmetrically;))


  • Registered Users Posts: 891 ✭✭✭Mmmm_Lemony


    Braces under! (Under)
    I probably should have asked if C was a first language and how long people have been programming for. At a wild guess I'd say under is the old school and on the same line is new school.

    I would have to agree that consistency is the most important factor. Python ftw!


  • Registered Users Posts: 15,065 ✭✭✭✭Malice


    Compile Error: braces.poll: Line 2: Don't trust a man who wears braces and a belt (Meh!0
    As whitespace isn't important, the question is, which style do you use, and if its the *under* style above....for the love of god WHY??!?
    I use the following in C#:

    [PHP]protected void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    // Stuff
    }
    }
    [/PHP]
    and the following in JavaScript:
    [PHP]<script type="text/javascript">
    $(document).ready(function () {
    // Stuff
    });
    </script>[/PHP]

    I find the first style a bit easier to follow but I'm not going to go on a mad reformatting spree if I join a project that uses the other style.

    As for why I use that style, I guess it's what I started with and I've never had a compelling reason to change.


  • Advertisement
  • Registered Users Posts: 11,980 ✭✭✭✭Giblet


    Malice wrote: »
    I use the following in C#:

    [PHP]protected void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    // Stuff
    }
    }
    [/PHP]
    and the following in JavaScript:
    [PHP]<script type="text/javascript">
    $(document).ready(function () {
    // Stuff
    });
    </script>[/PHP]

    I find the first style a bit easier to follow but I'm not going to go on a mad reformatting spree if I join a project that uses the other style.

    As for why I use that style, I guess it's what I started with and I've never had a compelling reason to change.

    I'm the same line on JavaScript too, just used to it I guess.


  • Registered Users Posts: 882 ✭✭✭moycullen14


    I probably should have asked if C was a first language and how long people have been programming for. At a wild guess I'd say under is the old school and on the same line is new school.

    I would have to agree that consistency is the most important factor. Python ftw!

    I think that's correct. Back in the day when procedural languages like C were the latest thing, you tended to have large functions/blocks (well, larger than today's java blocks). Also you were looking at code on smaller terminal windows (vt100 anyone?). If it was good enough for K&R, then it's good enough for me.....

    I still find that 'eyeballing' large sections of code is easier with under indentation. You automatically line up braces. Also, the single statement block (without braces) is easier to spot in an under indentation scheme.
    if (a)
        b();
    
    stands out as a single statement block (you can tell from indentation)
    if (a) {
       b();
    
    }

    is, for my taste, too similar.
    if (a)
    {
        b();
    }
    
    reads better.

    I would insist that everyone on projects I work on indents their code to my standards (which I may arbitrarily change). I also change peoples indentation and check their code back into code control hence messing up the change history. It's my way or the highway!


  • Registered Users Posts: 40,038 ✭✭✭✭Sparks


    Good grief.
    Gnu Indent (1989), Artistic Style (1997), Uncrustify (2006).
    How the hell is this still a concern today?


  • Registered Users Posts: 40,038 ✭✭✭✭Sparks


    At a wild guess I'd say under is the old school and on the same line is new school
    Just a minor bit: it's the other way round:
    functionname() {
        code
    }
    
    is the original K&R coding style as per The Book.
    functionname()
    {
        code
    }
    
    and about four different variants of it all with different names (Allman is the one I'm most used to hearing it called) all came later.


    I still say it's mad to think this is a thing when we've had tools that move from format to format for every language under the sun since before most of todays senior developers went to secondary school...


  • Registered Users Posts: 1,922 ✭✭✭fergalr


    Compile Error: braces.poll: Line 2: Don't trust a man who wears braces and a belt (Meh!0
    Well I'm a PHP guy mostly, so I try to stick to the Zend coding style, which uses a mixture of both depending on what you're doing, so you can end up with:
    [php]
    function blah()
    {
    if(x) {
    //
    } else {
    //
    }
    }
    [/php]

    TabsSpacesBoth.png


  • Registered Users Posts: 882 ✭✭✭moycullen14


    Sparks wrote: »
    Good grief.
    Gnu Indent (1989), Artistic Style (1997), Uncrustify (2006).
    How the hell is this still a concern today?

    http://www.haskell.org/haskellwiki/Wadlers_Law


  • Registered Users Posts: 1,922 ✭✭✭fergalr


    Compile Error: braces.poll: Line 2: Don't trust a man who wears braces and a belt (Meh!0
    I have a slight preference for matching braces being vertically aligned.
    This makes it easy to see where the blocks of code begin and end.

    People that prefer braces on the same line as the keyword, might say that the beginning of a block can easily be seen as the keyword directly above the closing brace. My problem with that is that, in many languages, blocks don't necessarily start with a keyword; so its not as consistent.

    You do spend an extra row of your terminal, if you put the opening brace on its own line; I think thats ok, we have big terminals, and its more important to be able to see structure.


    All that said though, I've written and read a lot of code in both styles, so I don't really mind.
    Its like anything, you get used to it over time, and pay more attention to semantics than syntax.

    Python don't have braces, and its very nice.
    Scheme (LISP) has brackets all over the place, and requires a different way of parsing brackets, again.

    Its all good.
    Like anything else, people should be consistent within a project, or an organisation, though.

    Either that or use editors that re-arrange syntax as they like, on checkout, (replacing the canonical rearrangement before checkin.)


    Its kind of bizarre, really. As programmers, we tend to be very good at separating presentation and logic.
    Its like an inbuilt architecture instinct we have.

    But, when you look at our coding tools, we haven't done this well at all.
    Surely all code should be represented in text in some canonical form, for storage and transmission, and then our editor just applies stylesheets? Some tools support this - but it really should be everywhere. What were we thinking?


  • Registered Users Posts: 40,038 ✭✭✭✭Sparks


    fergalr wrote: »
    Either that or use editors that re-arrange syntax as they like, on checkout, (replacing the canonical rearrangement before checkin.
    It's not the actual editor that should do this; we have hooks in source control programs to fire off tools like uncrustify on checkin/checkout/$OPERATION.
    That way, the moment you get the source, it's ready for use in the format you prefer no matter what tool you use on it (editor, diff, patch, etc, etc, etc) and when you check your code back in, it's converted to the nominally "official" format preferred by the team. And now nobody needs to worry about code formatting ever again. Solved problem.


  • Registered Users Posts: 1,922 ✭✭✭fergalr


    Compile Error: braces.poll: Line 2: Don't trust a man who wears braces and a belt (Meh!0
    Sparks wrote: »
    It's not the actual editor that should do this; we have hooks in source control programs to fire off tools like uncrustify on checkin/checkout/$OPERATION.
    That way, the moment you get the source, it's ready for use in the format you prefer no matter what tool you use on it (editor, diff, patch, etc, etc, etc) and when you check your code back in, it's converted to the nominally "official" format preferred by the team. And now nobody needs to worry about code formatting ever again. Solved problem.

    Yeah, exactly, thats the way it should be.

    I guess my point is, why isn't this pervasive yet? Should really have been around for 20 years; languages shouldn't really have syntax styles, and polls like this one shouldn't mean anything...

    Or, well, should just be questions about personal taste, but that doesn't really have consequence elsewhere. Maybe thats what it already is. I guess, ideally, our web browsers, OS etc would even have plugins, that whenever they see data that represents code, does the rendering and styling automatically. Sort of like a Unicode, but for code.


  • Registered Users Posts: 5,246 ✭✭✭conor.hogan.2


    Compile Error: braces.poll: Line 2: Don't trust a man who wears braces and a belt (Meh!0
    The differences are so small I would never bother to change on checkout even though it would be automatic.

    Changing between languages, browsers, operating systems and keyboard layouts is done often I can't see why minimal indenting would be a problem enough to even automate it.


  • Advertisement
  • Moderators, Society & Culture Moderators Posts: 9,689 Mod ✭✭✭✭stevenmu


    Doing Java in college, the examples I saw were always on the same line, so that's what I did.

    When I started C# the examples were always under the line, so that's what I did. It was weird at first, but I've come to prefer this as I find it slightly quicker to match braces. I know VS has highlighting, but when they're on the same page it's quicker to just spot them at a glance.

    I read an article a while ago though about why 'same line' brace are important in Javascript (article here), so I started using that for any Javascript I do. even though it was the style I started with, it was kinda weird at first, but I got used to it quick enough and now I'm happy doing C# one way and Javascript the other.


  • Registered Users Posts: 15,065 ✭✭✭✭Malice


    Compile Error: braces.poll: Line 2: Don't trust a man who wears braces and a belt (Meh!0
    I would insist that everyone on projects I work on indents their code to my standards (which I may arbitrarily change). I also change peoples indentation and check their code back into code control hence messing up the change history. It's my way or the highway!
    The above is a joke isn't it?


Advertisement