View Poll Results: Which style do you prefer?

Voters
54. You may not vote on this poll
  • K&R - the One True Brace Style!

    25 46.30%
  • ANSI and ISO - get with the times you old dogs!

    26 48.15%
  • Screw the standards - I have my own! (Please describe it.)

    3 5.56%
Page 1 of 4 123 ... LastLast
Results 1 to 10 of 33

Thread: [C++} Brace Wars: K&R vs ANSI/ISO

  1. #1
    Join Date
    Dec 2006
    Location
    Australia
    Beans
    1,097
    Distro
    Ubuntu 12.04 Precise Pangolin

    [C++] Brace Wars: K&R vs ANSI/ISO

    Most of us will be aware that there are two very popular styles of C++ coding: K&R and ANSI.

    On the whole, they aren't really all that much different from each other, and have their merits.

    The most visible and obvious difference is, I think, the treatment of whitespace and the use of braces in compound statements.

    Some examples (surrounding code omitted for brevity):

    K&R style:
    Code:
        while (j != 1) {
            i++;
            if (i == 100) {
                j = 1;
            }
        }
    ANSI and ISO C++ style:
    Code:
        while (j != 1)
        {
            i++;
            if (i == 100)
            {
                j = 1;
            }
        }
    With C, the choice is clear. ANSI/ISO seem to follow the K&R style in its C standard documentation.

    With C++, ANSI/ISO differ from K&R. ANSI/ISO have a coding style which advocates placing the opening "{" brace on a line of its own.

    Which style is your preference?

    For me, I prefer K&R due to my C and Java background (note: Java uses a style similar to K&R but not identical). ANSI/ISO's style appears to waste too much space. I appreciate the clarity of having the opening brace on its own line, but it is usually unnecessary.
    Last edited by samjh; April 18th, 2008 at 03:46 PM.

  2. #2
    Join Date
    Oct 2004
    Beans
    1,532

    Re: [C++} Brace Wars: K&R vs ANSI/ISO

    Years ago I settled on this style:
    PHP Code:
        while (!= 1)
            {
            
    i++;
            if (
    == 100)
                {
                
    1;
                }
            } 
    I just didn't like those braces lined up immediately under the key words "if", "while", etc, in the ANSI style, and I didn't like the dangling closing braces of K&R. (And the obsessive/compulsive part of me didn't like that function definitions generally didn't follow the style. )

    But as long as a style is used consistently within a piece of code, I don't really care. If I modify someone else's code, I stay with the existing style (if there is one).
    Last edited by WW; April 18th, 2008 at 02:55 PM.

  3. #3
    Join Date
    Dec 2006
    Location
    Glasgow, Scotland
    Beans
    470
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: [C++} Brace Wars: K&R vs ANSI/ISO

    Quote Originally Posted by samjh View Post
    Which style is your preference?
    I'm with you. K&R pleases my sensibilities. However, I've got used to reading the other styles, even if I don't like them.

    Whiltesmith's style particularly offends me.

    EDIT: some of the best people use Whitesmiths
    Last edited by ruy_lopez; April 18th, 2008 at 03:09 PM.
    Don't be too sure every 'thanks' you receive is genuine.

  4. #4
    Join Date
    Oct 2004
    Beans
    1,532

    Re: [C++} Brace Wars: K&R vs ANSI/ISO

    Quote Originally Posted by ruy_lopez View Post
    Whiltesmith's style particularly offends me.
    Sorry, dude!

    Heh... I didn't know it had a name.
    Last edited by WW; April 18th, 2008 at 03:02 PM.

  5. #5
    Join Date
    Dec 2006
    Location
    Australia
    Beans
    1,097
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: [C++} Brace Wars: K&R vs ANSI/ISO

    Quote Originally Posted by ruy_lopez View Post
    I'm with you. K&R pleases my sensibilities. However, I've got used to reading the other styles, even if I don't like them.
    That is an important point. Even if one doesn't like a style, one should be able to decipher it fluently.

    Although I'm not a big fan of the ANSI/ISO style (or Allman's, according to that Wikipedia link), I do appreciate its uses, especially when writing Ada code (you've got no choice with Ada, as it uses a comb format with begin...end instead of { } ).

  6. #6
    Join Date
    Apr 2007
    Beans
    14,781

    Re: [C++} Brace Wars: K&R vs ANSI/ISO

    I don't care as long as it consistant. As long as the indents make sense (like Python) the braces are just there to take up space.

    I use:
    Code:
    for (i = 0; i < MAXLENGTH; i++)
    {
        printf("%d\n",i);
    }
    
    //have used this before, I think it is a good style
    for (i = 0; i < MAXLENGTH; i++)
      {
        printf("%d\n",i);
      }
    I really don't see a point of the "K&R" style, yes, it saves one line, but why have the other brace on a line?

    This would make more sense:
    Code:
    for (i = 0; i < MAXLENGTH; i++){
        printf("%d\n",i);}
    Last edited by LaRoza; April 18th, 2008 at 03:12 PM.

  7. #7
    Join Date
    Dec 2006
    Location
    Australia
    Beans
    1,097
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: [C++} Brace Wars: K&R vs ANSI/ISO

    Quote Originally Posted by LaRoza View Post
    I really don't see a point of the "K&R" style, yes, it saves one line, but why have the other brace on a line?

    This would make more sense:
    Code:
    for (i = 0; i < MAXLENGTH; i++){
        printf("%d\n",i);}
    Because unlike Python and languages with significant whitespace, C/C++ is freeform. Programmers who are used to freeform languages rely on start-end symbols/keywords to keep track of statement blocks.

    In C/C++/C#/Java and other C-like languages, the marking of statement blocks is facilitated by { and }. In Pascal, Ada, and other ALGOL-like languages, the begin...end serves the purpose.

    A C/C++ programmer will know that do, while, for, if, and switch statements will always begin a statement block. So there is no need to separate the body of the block with a lone opening brace; therefore the opening brace can be placed on the same line as the do/while/for/if/switch statement. But the final statement of a block is unpredictable, so you need a clear delimiter to mark the end of a block. A closing brace on the same line as the final statement in a block is not clear, but having the closing brace on a line of its own cannot be mistaken.

    The Allman/ANSI/ISO style uses a lone opening brace to enhance clarity even further. This is good for writing very large programs with large source files, or for safety critical programs when code-maintainability is essential (one of the reasons why Ada was chosen for its role was its syntax was ALGOL-based with English words to very clearly separate blocks of statements and other portions of code, and therefore very easy to decipher even for non-programmers).
    Last edited by samjh; April 18th, 2008 at 03:47 PM.

  8. #8
    Join Date
    Apr 2007
    Beans
    14,781

    Re: [C++} Brace Wars: K&R vs ANSI/ISO

    Quote Originally Posted by samjh View Post
    Because unlike Python and languages with significant whitespace, C/C++ is freeform. Programmers who are used to freeform languages rely on start-end symbols/keywords to keep track of statement blocks.

    The Allman/ANSI/ISO style uses a lone opening brace to enhance clarity even further. This is good for writing very large programs with large source files, or for safety critical programs when code-maintainability is essential (one of the reasons why Ada was chosen for its role was its syntax was ALGOL-based, and therefore very easy to track and decipher even for non-programmers).
    I disagree. Programmers who are used to freeform languages rely on indents, not start-end symbols.

    There is no way a programmer could find this a readable format:

    Code:
    for
    (i = 0;
    i < MAXLENGth
    ;i++){printf("%d\n",i);}
    This has all the requirements. The compiler relies on the start-end symbols, programmers rely on the style (using indents)

  9. #9
    Join Date
    Dec 2006
    Location
    Australia
    Beans
    1,097
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: [C++} Brace Wars: K&R vs ANSI/ISO

    Quote Originally Posted by LaRoza View Post
    I disagree. Programmers who are used to freeform languages rely on indents, not start-end symbols.

    There is no way a programmer could find this a readable format:

    Code:
    for
    (i = 0;
    i < MAXLENGth
    ;i++){printf("%d\n",i);}
    This has all the requirements. The compiler relies on the start-end symbols, programmers rely on the style (using indents)
    I think you misunderstand me.

    I did not say that programmers of freeform language rely exclusively on start-end symbols/words. They simply rely on them - meaning there are other factors involved, such as indents as you pointed out.

    Your example is very extreme and does not fit the requirements of readability at all. There is no consistency, no clear grouping of code, and no clear separation between logical groups of code. You merely pointed out that symbols alone cannot make code readable, which is not the point of K&R or any other sensible coding style. Readability is a combination of many things, of which grouping and separation of statement blocks is one of them.

    The part of my post which you omitted should explain why the bracing style of K&R is what it is. It's not necessarily the best, but it is one of the better styles for C/C++. It is personally my favoured style.

    It needn't make sense to you in particular. But it does make a lot of sense to most.
    Last edited by samjh; April 18th, 2008 at 04:06 PM.

  10. #10
    Join Date
    May 2007
    Location
    Paris, France
    Beans
    927
    Distro
    Kubuntu 7.04 Feisty Fawn

    Re: [C++} Brace Wars: K&R vs ANSI/ISO

    ANSI/ISO style here.
    Not even tinfoil can save us now...

Page 1 of 4 123 ... LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •