Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. What are your curly-bracing style?

What are your curly-bracing style?

Scheduled Pinned Locked Moved The Lounge
cssjavaadobequestion
99 Posts 49 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z ZanyZapper

    _Maxxx_ wrote:

    Some of your reasoning is valid (in my view) but I always think that this obsession some programmers have with the reduction in keystrokes (I'd have to press TAB all the time to indent, I don't want to type two extra braces if I don't need to etc.) is plain silly.

    +10000 And yet some of these same programmers will UseCrazyLongMethodNamesLongerThanTheMethod unnecessarily. Something I'd like to see in an IDE is separate view and save format options. Open a file for editing, it reformats to my liking. Hit save and what gets written to disk is formatted to whatever the rest of the team wants to see, or some corporate standard, or whatever. Could make debugging tricky, especially if line numbers change. Or maybe put that functionality into the source management. Repository contains some canonical form of the code (maybe even an XML representation or something else) and when checked out your desired format is applied. Check in would convert back to the canonical form. Almost like using CSS to style HTML.

    P Offline
    P Offline
    PIEBALDconsult
    wrote on last edited by
    #78

    ZanyZapper wrote:

    put that functionality into the source management.

    Yes, the code should be formatted to the company standard before determining whether or not there are any changes.

    1 Reply Last reply
    0
    • P Peter Mulholland

      I hope I never have to maintain any code you've written. This:

      nikunjbhatt84 wrote:

      if(a>b)

      is just wrong. Use some whitespace!

      if (a > b)
      {
      }

      Pete

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #79

      Indeed, the only place I've worked that had (has?) a standard, specified single spaces around all operators (or maybe even tokens).

      1 Reply Last reply
      0
      • J Jim SS

        I vote for getting rid of braces and using indentation exclusively. Using braces the way many people do causes too many wasted lines (empty of anything useful).

        SS => Qualified in Submarines "We sleep soundly in our beds because rough men stand ready in the night to visit violence on those who would do us harm". Winston Churchill "Real programmers can write FORTRAN in any language". Unknown

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #80

        Jim (SS) wrote:

        (empty of anything useful).

        They contain elbow room; we all need a little elbow room...

        1 Reply Last reply
        0
        • N Nikunj_Bhatt

          Which method do u use for curly braces to create scope of a programming structure? I mostly prefer this method:

          if(a>b)
          { // sometimes i write comment here about logic and parameters etc.
          print "b is less than or equal to a"
          print "it means a is greater than b"
          }
          else
          print "a is either either equal to or less than b"

          Note that, I don't use braces for a single line of scope and I indent the starting brace and ending brace and it is not on the same line where the control structure is defined. I use this approach because it makes easy (just hit Enter key, no need to press Tab key) to add a new line of code after the staring brace and before the first statement of the block. I use Notepad++ and it has slightly good matching braces hilting feature and this method helps to correctly lineup and identify scope content. Here are some more methods used my many programmers:

          if(a>b) { // this is Flash's ActionScript's default formatting, I hate this style the most, I feel it most unreadable, some Java programmers and web designers working on CSS also use almost similar method for writing CSS rules
          print "b is less than or equal to a"
          print "it means a is greater than b"
          } else {
          print "a is either either equal to or less than b"
          }

          if(a>b)
          {
          print "b is less than or equal to a"
          print "it means a is greater than b"
          }
          else
          {
          print "a is either either equal to or less than b"
          }

          if(a>b)
          { print "b is less than or equal to a"
          print "it means a is greater than b"
          }

          M Offline
          M Offline
          Mr Crisp
          wrote on last edited by
          #81

          I'm currently maintaining a code base that has this style;- if(a>b) DoFunction(); which I find massively annoying, when debugging and stepping through the code, as you can't put a break point on a positive condition result.:mad:

          1 Reply Last reply
          0
          • J jschell

            Joe Woodbury wrote:

            Aaahhh, the other developer on earth that uses this horrid style. Our main developer uses this and we never let him forget how unreadable it is.

            Please provide the objective method that you use to determine how something is "readable" in a positive or negative way. Also provide the measured impact that it has on productivity. I would also like to see the impact that your complaints about the code has on productivity as well.

            G Offline
            G Offline
            Gary R Wheeler
            wrote on last edited by
            #82

            There is no 'objective' method for determining readability, since readability is inherently subjective. You could do a survey of 100 programmers to judge the readability of a body of code, and all you have at the end is a statistical result, averaging their opinions. I don't find one brace style particularly more readable than another. Consistency of style drives readability more than anything else for me.

            Software Zen: delete this;
            Fold With Us![^]

            J 1 Reply Last reply
            0
            • C Colin Rae

              The proper way.

              if (something){
                  do stuff
                  do more stuff
              }
              else{
                  do something different
              }
              

              Am I showing my age if I mention Kernighan and Ritchie? :)

              G Offline
              G Offline
              Gary R Wheeler
              wrote on last edited by
              #83

              My older stuff uses K&R, since that's what I started it in. Nowadays I tend to do this:

              if (a < b)
              {
              DoStuff();
              }
              else
              {
              DoOtherStuff();
              }

              I need the added white space due to my weak, middle-aged vision :-O .

              Software Zen: delete this;
              Fold With Us![^]

              1 Reply Last reply
              0
              • P PIEBALDconsult

                That's what I've heard too, but Ritchie's 1974 document has it the "K&R" way:

                struct tnode {
                char tword[20];
                int count;
                struct tnode *left;
                struct tnode *right;
                };

                (Six SPACES!) And Thompson's B document has:

                switch(x) {
                case ’a’:
                y = 1 ;
                case ’b’:
                z = 2;
                }

                X| (Three SPACES!) It appears that BCPL has neither braces nor semi-colons.

                D Offline
                D Offline
                Dan Neely
                wrote on last edited by
                #84

                PIEBALDconsult wrote:

                That's what I've heard too, but Ritchie's 1974 document has it the "K&R" way:

                Might be an urban legend. I oculdn't find anything authoritative when I googled.

                PIEBALDconsult wrote:

                It appears that BCPL has neither braces nor semi-colons.

                The wikipedia article[^] lists it a the 1st curly brace language and includes a few examples which use them. The intending is even nastier than your B example and looks like someone gorged on a mix of basic, pascal, and C and vomited it all up again. X|

                GET "libhdr"

                GLOBAL { count:200; all:201 }

                LET try(ld, row, rd) BE TEST row=all

                                    THEN count := count + 1
                
                                    ELSE { LET poss = all & ~(ld | row | rd)
                                           UNTIL poss=0 DO
                                           { LET p = poss & -poss
                                             poss := poss - p
                                             try(ld+p << 1, row+p, rd+p >> 1)
                                           }
                                         }
                

                LET start() = VALOF
                { all := 1

                FOR i = 1 TO 12 DO
                { count := 0
                try(0, 0, 0)
                writef("Number of solutions to %i2-queens is %i5*n", i, count)
                all := 2*all + 1
                }

                RESULTIS 0
                }

                3x12=36 2x12=24 1x12=12 0x12=18

                J P 2 Replies Last reply
                0
                • P PIEBALDconsult

                  In the former, you can #if out the else on its own (without the braces).

                  J Offline
                  J Offline
                  Joe Klemmer
                  wrote on last edited by
                  #85

                  True enough. But I prefer to put braces around every statement block, even if it is just one line. e.g.

                  if (a == 0)
                  {
                  x += 1;
                  }

                  This keeps me from doing this -

                  if (a == 0)
                  x += 1;
                  return 1;

                  when I mean -

                  if (a == 0)
                  {
                  x += 1;
                  return 1;
                  }

                  if changes need to be made.

                  -- http://ohai.im/joe.klemmer

                  P 1 Reply Last reply
                  0
                  • J jschell

                    Places where I work have more important things to do that make observations on style structures that have nothing at all do to with functionality nor productivity.

                    J Offline
                    J Offline
                    Joe Klemmer
                    wrote on last edited by
                    #86

                    Are you saying that the places you work have no concern for coding standards and code maintainability? At least that's what it sounds like you're saying. If so, I wouldn't want to work there. I've had me share of dealing with 20 different code formatting styles.

                    -- http://ohai.im/joe.klemmer

                    J 1 Reply Last reply
                    0
                    • M Michael Waters

                      I also put each brace on its own line (although I will also put comments on that line), aligned with the statement begining the block, and indenting lines within that block. Likewise, I DO brace single line statements. Why? Because it makes adding console output debugging statements (like TRACE()) to the code far less perilous. I absoulutely hate the java convention of the first brace at the end of the invoking statement's line, but the end brace on its own line. If only C++ hadn't insisted on being a superset of C, much good would have been accomplished. Maybe it could ditch across-the-board C compatibility when the NEXT standard is released, and do things like REQUIRE braces around single line blocks. Maybe then we could answer the question once and for all whether

                      int* pInt;
                      long& rLong;

                      or

                      int *pInt;
                      long &rLong;

                      , or even

                      int * pInt;
                      long & rLong;

                      should be the one and only correct way to decalre a pointer or reference.

                      J Offline
                      J Offline
                      jsc42
                      wrote on last edited by
                      #87

                      Michael Waters wrote:

                      I absoulutely hate the java convention of the first brace at the end of the invoking statement's line, but the end brace on its own line.

                      It's not a Java convention it is a K&R (Kernigan and Ritchie) convention for C that many Java shops that just follow trends rather than thinking for themselves have inheritted. It has a highly arrogant and presumptuative name: TOOTBS (The One and Only True Brace Style). Personally, I think it should be called Really Unimaginably Bad Brace Indenting Style Hack (or RUBBISH for short - I need a better 'H' [suggestions?]). I am in the process of reading the C# book in the O'Reilly series and I am pleased to report that its authors use open braces aligned with close braces even with Visual Studio. Perhaps we ought to be equally as arrogant as K&R and call aligned braces TOOLBS (The One and Only Logical Brace Style) or TOOSBS (The One and Only Sensible Brace Style). :~

                      1 Reply Last reply
                      0
                      • P Peter Mulholland

                        I hope I never have to maintain any code you've written. This:

                        nikunjbhatt84 wrote:

                        if(a>b)

                        is just wrong. Use some whitespace!

                        if (a > b)
                        {
                        }

                        Pete

                        N Offline
                        N Offline
                        Nikunj_Bhatt
                        wrote on last edited by
                        #88

                        Peter Mulholland wrote:

                        I hope I never have to maintain any code you've written. This: nikunjbhatt84 wrote: if(a>b) is just wrong. Use some whitespace! if (a > b) { }

                        I use whitespace only when if I think the code would look complex without whitespace.

                        1 Reply Last reply
                        0
                        • P Peter_Olson

                          This doesn't relate to the curly braces, but your logic is incorrect. If a>b, b cannot be equal to a. Thus, "b is less than or equal to a" does not mean the same thing as "a is greater than b".

                          N Offline
                          N Offline
                          Nikunj_Bhatt
                          wrote on last edited by
                          #89

                          peterofthecorn wrote:

                          This doesn't relate to the curly braces, but your logic is incorrect. If a>b, b cannot be equal to a. Thus, "b is less than or equal to a" does not mean the same thing as "a is greater than b".

                          ya, u have spotted the wrong thing rightly. i just want to put something funny/rubbish, so in hurry, i have written what u have spotted. good observation!!

                          1 Reply Last reply
                          0
                          • N Nikunj_Bhatt

                            Which method do u use for curly braces to create scope of a programming structure? I mostly prefer this method:

                            if(a>b)
                            { // sometimes i write comment here about logic and parameters etc.
                            print "b is less than or equal to a"
                            print "it means a is greater than b"
                            }
                            else
                            print "a is either either equal to or less than b"

                            Note that, I don't use braces for a single line of scope and I indent the starting brace and ending brace and it is not on the same line where the control structure is defined. I use this approach because it makes easy (just hit Enter key, no need to press Tab key) to add a new line of code after the staring brace and before the first statement of the block. I use Notepad++ and it has slightly good matching braces hilting feature and this method helps to correctly lineup and identify scope content. Here are some more methods used my many programmers:

                            if(a>b) { // this is Flash's ActionScript's default formatting, I hate this style the most, I feel it most unreadable, some Java programmers and web designers working on CSS also use almost similar method for writing CSS rules
                            print "b is less than or equal to a"
                            print "it means a is greater than b"
                            } else {
                            print "a is either either equal to or less than b"
                            }

                            if(a>b)
                            {
                            print "b is less than or equal to a"
                            print "it means a is greater than b"
                            }
                            else
                            {
                            print "a is either either equal to or less than b"
                            }

                            if(a>b)
                            { print "b is less than or equal to a"
                            print "it means a is greater than b"
                            }

                            N Offline
                            N Offline
                            Nikunj_Bhatt
                            wrote on last edited by
                            #90

                            Thank you all for wasting some of your time here ;) However this was just a time-pass post from me, I got many responses and many of them very logical and informative. Many have talked about being consistent and getting used to any style if we are working on code which is coded by someone else. These are really nice approaches. As I am a teacher, I tell my students to better indent the code in whatever style and staying consistent with it. Sometimes its very hard to spot even a small silly mistake done by them if they have not formatted the code in any consistent way. BTW, some have spotted logic error in my sample code. Thumbs UP to them :thumbsup:.

                            1 Reply Last reply
                            0
                            • J Joe Klemmer

                              Are you saying that the places you work have no concern for coding standards and code maintainability? At least that's what it sounds like you're saying. If so, I wouldn't want to work there. I've had me share of dealing with 20 different code formatting styles.

                              -- http://ohai.im/joe.klemmer

                              J Offline
                              J Offline
                              jschell
                              wrote on last edited by
                              #91

                              Joe Klemmer wrote:

                              Are you saying that the places you work have no concern for coding standards and code maintainability

                              The first in terms of this discussion has no provable impact on the second.

                              Joe Klemmer wrote:

                              I've had me share of dealing with 20 different code formatting styles.

                              I had my share of dealing with C++ programmers that do not deal with pointers consistently or use them incorrectly. I had my share of dealing with programmers in C++, C# and Java who do not deal with error scenarios at all. Or who deal with them incorrectly. I had my share of dealing with programmers that do not understand specifications, literally cannot follow specifications, ignore specifications, do not know specifications exist or assume that specifications are 100% correct. I had my share of dealing with programmers that think inheritance is used to provide code sharing. etc, etc, etc... All of those have had a measurable and significant impact on quality, performance and maintainability. Conversely I have never, not a single time, had a single situation where bracket placement had any impact on any project. And thus it follows that it never had a measurable or significant impact. I have however also seen programmers that think that code standards have some impact on code quality and yet are quite willing to ignore or even disparage process control techniques which have a proven impact.

                              1 Reply Last reply
                              0
                              • D Dan Neely

                                PIEBALDconsult wrote:

                                That's what I've heard too, but Ritchie's 1974 document has it the "K&R" way:

                                Might be an urban legend. I oculdn't find anything authoritative when I googled.

                                PIEBALDconsult wrote:

                                It appears that BCPL has neither braces nor semi-colons.

                                The wikipedia article[^] lists it a the 1st curly brace language and includes a few examples which use them. The intending is even nastier than your B example and looks like someone gorged on a mix of basic, pascal, and C and vomited it all up again. X|

                                GET "libhdr"

                                GLOBAL { count:200; all:201 }

                                LET try(ld, row, rd) BE TEST row=all

                                                    THEN count := count + 1
                                
                                                    ELSE { LET poss = all & ~(ld | row | rd)
                                                           UNTIL poss=0 DO
                                                           { LET p = poss & -poss
                                                             poss := poss - p
                                                             try(ld+p << 1, row+p, rd+p >> 1)
                                                           }
                                                         }
                                

                                LET start() = VALOF
                                { all := 1

                                FOR i = 1 TO 12 DO
                                { count := 0
                                try(0, 0, 0)
                                writef("Number of solutions to %i2-queens is %i5*n", i, count)
                                all := 2*all + 1
                                }

                                RESULTIS 0
                                }

                                3x12=36 2x12=24 1x12=12 0x12=18

                                J Offline
                                J Offline
                                jschell
                                wrote on last edited by
                                #92

                                Dan Neely wrote:

                                Might be an urban legend. I oculdn't find anything authoritative when I googled.

                                True. I spent some time one time trying to validate it but couldn't find anything. I thought that I read a micro interview in the magazine "C++ Reports" (believe that was the name) years ago that said that.

                                1 Reply Last reply
                                0
                                • J Joe Woodbury

                                  Please get a sense of humor.

                                  J Offline
                                  J Offline
                                  jschell
                                  wrote on last edited by
                                  #93

                                  Joe Woodbury wrote:

                                  Please get a sense of humor.

                                  I do laugh when I see people make statements that suggest that minor style elements have any measurable impact in software development. I don't laugh so much when some of those same people dismiss process control idioms that have a proven measurable and significant impact and instead think that a coding guideline by itself is going to having any positive impact on the code. I have attempted to find supporting evidence that suggests anything about style elements and have never found one (excluding one that showed that in written material using excessive fonts will have a negative impact.) Conversely there are probably hundreds if not thousands of studies about different process control practices with produced measured benefits.

                                  1 Reply Last reply
                                  0
                                  • G Gary R Wheeler

                                    There is no 'objective' method for determining readability, since readability is inherently subjective. You could do a survey of 100 programmers to judge the readability of a body of code, and all you have at the end is a statistical result, averaging their opinions. I don't find one brace style particularly more readable than another. Consistency of style drives readability more than anything else for me.

                                    Software Zen: delete this;
                                    Fold With Us![^]

                                    J Offline
                                    J Offline
                                    jschell
                                    wrote on last edited by
                                    #94

                                    Gary R. Wheeler wrote:

                                    There is no 'objective' method for determining readability, since readability is inherently subjective. You could do a survey of 100 programmers to judge the readability of a body of code, and all you have at the end is a statistical result, averaging their opinions.

                                    I am fairly certain that one would find that say at least 90%+ of programmers would prefer code that has some white space in it to code that has none. And quite a few wouldn't care for code that has a large amount either. I am also fairly certain that probably something like 99%+ of people would find that something like user manual that uses a 3 point font would not be acceptable. It is certainly possible that a study that was used to test that might use the term 'readable' in qualifying preferences. I do not think that anyone has tried to measure that however.

                                    G 1 Reply Last reply
                                    0
                                    • J jschell

                                      Gary R. Wheeler wrote:

                                      There is no 'objective' method for determining readability, since readability is inherently subjective. You could do a survey of 100 programmers to judge the readability of a body of code, and all you have at the end is a statistical result, averaging their opinions.

                                      I am fairly certain that one would find that say at least 90%+ of programmers would prefer code that has some white space in it to code that has none. And quite a few wouldn't care for code that has a large amount either. I am also fairly certain that probably something like 99%+ of people would find that something like user manual that uses a 3 point font would not be acceptable. It is certainly possible that a study that was used to test that might use the term 'readable' in qualifying preferences. I do not think that anyone has tried to measure that however.

                                      G Offline
                                      G Offline
                                      Gary R Wheeler
                                      wrote on last edited by
                                      #95

                                      My point was that brace style in and of itself is not a primary factor in readability for most programmers. Other factors like number of lines per function, indentation, and naming conventions are bigger considerations. I've read code written with K&R braces and no white space that was eminently readable. I've also read code with braces on new lines by themselves that I couldn't follow at all.

                                      Software Zen: delete this;
                                      Fold With Us![^]

                                      J 1 Reply Last reply
                                      0
                                      • D Dan Neely

                                        PIEBALDconsult wrote:

                                        That's what I've heard too, but Ritchie's 1974 document has it the "K&R" way:

                                        Might be an urban legend. I oculdn't find anything authoritative when I googled.

                                        PIEBALDconsult wrote:

                                        It appears that BCPL has neither braces nor semi-colons.

                                        The wikipedia article[^] lists it a the 1st curly brace language and includes a few examples which use them. The intending is even nastier than your B example and looks like someone gorged on a mix of basic, pascal, and C and vomited it all up again. X|

                                        GET "libhdr"

                                        GLOBAL { count:200; all:201 }

                                        LET try(ld, row, rd) BE TEST row=all

                                                            THEN count := count + 1
                                        
                                                            ELSE { LET poss = all & ~(ld | row | rd)
                                                                   UNTIL poss=0 DO
                                                                   { LET p = poss & -poss
                                                                     poss := poss - p
                                                                     try(ld+p << 1, row+p, rd+p >> 1)
                                                                   }
                                                                 }
                                        

                                        LET start() = VALOF
                                        { all := 1

                                        FOR i = 1 TO 12 DO
                                        { count := 0
                                        try(0, 0, 0)
                                        writef("Number of solutions to %i2-queens is %i5*n", i, count)
                                        all := 2*all + 1
                                        }

                                        RESULTIS 0
                                        }

                                        3x12=36 2x12=24 1x12=12 0x12=18

                                        P Offline
                                        P Offline
                                        PIEBALDconsult
                                        wrote on last edited by
                                        #96

                                        Regarding BCPL; I didn't read the document I have (Richards 1967) too closely, but it seems to be even worse -- it's not necessarily braces; his examples generally use $( and $) instead and the rule seems to be "anything that matches". Plus a "closing section bracket" can close multiple sections... a la LISP. X| As to semi-colons, they are mentioned and defined, but don't seem to be demonstrated in the examples. Certainly they are not used as they are in C.

                                        D 1 Reply Last reply
                                        0
                                        • J Joe Klemmer

                                          True enough. But I prefer to put braces around every statement block, even if it is just one line. e.g.

                                          if (a == 0)
                                          {
                                          x += 1;
                                          }

                                          This keeps me from doing this -

                                          if (a == 0)
                                          x += 1;
                                          return 1;

                                          when I mean -

                                          if (a == 0)
                                          {
                                          x += 1;
                                          return 1;
                                          }

                                          if changes need to be made.

                                          -- http://ohai.im/joe.klemmer

                                          P Offline
                                          P Offline
                                          PIEBALDconsult
                                          wrote on last edited by
                                          #97

                                          Yes, as do I, but they each have their own line (unless they're on the same line) -- {} .

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups