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.
  • S Soulus83

    Code block is so long I can't see the voting bar! This is my preferred style:

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

    Each curly brace on it's own line, don't know why but it helps me read the code :confused:

    R Offline
    R Offline
    realJSOP
    wrote on last edited by
    #5

    +1

    ".45 ACP - because shooting twice is just silly" - JSOP, 2010
    -----
    You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
    -----
    "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997

    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"
      }

      J Offline
      J Offline
      Joe Woodbury
      wrote on last edited by
      #6

      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.

      N J 2 Replies Last reply
      0
      • S Soulus83

        Code block is so long I can't see the voting bar! This is my preferred style:

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

        Each curly brace on it's own line, don't know why but it helps me read the code :confused:

        N Offline
        N Offline
        Nish Nishant
        wrote on last edited by
        #7

        That's my curly brace style too.

        Regards, Nish


        My technology blog: voidnish.wordpress.com

        A 1 Reply Last reply
        0
        • N Nish Nishant

          That's my curly brace style too.

          Regards, Nish


          My technology blog: voidnish.wordpress.com

          A Offline
          A Offline
          AspDotNetDev
          wrote on last edited by
          #8

          I'll tell you my style, but you should brace yourself first.

          [WikiLeaks Cablegate Cables]

          1 Reply Last reply
          0
          • S Soulus83

            Code block is so long I can't see the voting bar! This is my preferred style:

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

            Each curly brace on it's own line, don't know why but it helps me read the code :confused:

            Steve EcholsS Offline
            Steve EcholsS Offline
            Steve Echols
            wrote on last edited by
            #9

            +3 I'm a white space fanatic! :)


            - S 50 cups of coffee and you know it's on! Code, follow, or get out of the way.

            • S
              50 cups of coffee and you know it's on!
              Code, follow, or get out of the way.
            R 1 Reply Last reply
            0
            • Steve EcholsS Steve Echols

              +3 I'm a white space fanatic! :)


              - S 50 cups of coffee and you know it's on! Code, follow, or get out of the way.

              R Offline
              R Offline
              realJSOP
              wrote on last edited by
              #10

              Steve Echols wrote:

              I'm a white space fanatic!

              That makes you a bracist...

              ".45 ACP - because shooting twice is just silly" - JSOP, 2010
              -----
              You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
              -----
              "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997

              Steve EcholsS H K 3 Replies 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"
                }

                R Offline
                R Offline
                realJSOP
                wrote on last edited by
                #11

                nikunjbhatt84 wrote:

                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"

                That will just get you laughed at and generally ridiculed in our shop...

                ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                -----
                You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                -----
                "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997

                J 1 Reply Last reply
                0
                • R realJSOP

                  Steve Echols wrote:

                  I'm a white space fanatic!

                  That makes you a bracist...

                  ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                  -----
                  You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                  -----
                  "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997

                  Steve EcholsS Offline
                  Steve EcholsS Offline
                  Steve Echols
                  wrote on last edited by
                  #12

                  Nice one! :thumbsup:


                  - S 50 cups of coffee and you know it's on! Code, follow, or get out of the way.

                  • S
                    50 cups of coffee and you know it's on!
                    Code, follow, or get out of the way.
                  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"
                    }

                    C Offline
                    C Offline
                    Colin Rae
                    wrote on last edited by
                    #13

                    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? :)

                    L D G 3 Replies 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? :)

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #14

                      Colin Rae wrote:

                      Am I showing my age if I mention Kernighan and Ritchie

                      Yes Incidentally the C++ guy (am I showing my age if I have forgotten his name) used

                      if (something)
                      {
                      DoSomething();
                      }
                      else
                      {
                      DoSomethingElse();
                      }

                      (not sure on the bracketed single line though)

                      ___________________________________________ .\\axxx (That's an 'M')

                      C 1 Reply Last reply
                      0
                      • J Joe Woodbury

                        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.

                        N Offline
                        N Offline
                        Nemanja Trifunovic
                        wrote on last edited by
                        #15

                        Joe Woodbury wrote:

                        Aaahhh, the other developer on earth that uses this horrid style

                        I know quite a few.

                        utf8-cpp

                        1 Reply Last reply
                        0
                        • L Lost User

                          Colin Rae wrote:

                          Am I showing my age if I mention Kernighan and Ritchie

                          Yes Incidentally the C++ guy (am I showing my age if I have forgotten his name) used

                          if (something)
                          {
                          DoSomething();
                          }
                          else
                          {
                          DoSomethingElse();
                          }

                          (not sure on the bracketed single line though)

                          ___________________________________________ .\\axxx (That's an 'M')

                          C Offline
                          C Offline
                          Colin Rae
                          wrote on last edited by
                          #16

                          I much prefer the opening brace on the same line as the statement it belongs to. Just seems nicer to me - I see an if or whatever and look for the corresponding closing brace below it. If I really wanted to show my age, I'd mention the fact that in first year at uni, we used DECWRITER IV hard copy terminals (for you young 'uns, that's basically a printer instead of a monitor)! After doing some course work in the evening, you'd end up going home carrying about half a tree! I missed punched cards by about 2 years - but my older brother used them!

                          L 1 Reply Last reply
                          0
                          • S Steve Mayfield

                            I used the 1st alt method many years, but now use the 2nd alt method as it is much easier (for me) to see the block structure. I use an indent with 2 spaces per level

                            Steve _________________ I C(++) therefore I am

                            M Offline
                            M Offline
                            Mycroft Holmes
                            wrote on last edited by
                            #17

                            Steve Mayfield wrote:

                            I use an indent with 2 spaces per level

                            First thing I do when a new dev comes on board, change ALL your indents to 2 spaces. There is nothing worse than picking up some code with a mixture of indents.

                            Never underestimate the power of human stupidity RAH

                            L 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"
                              }

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #18

                              My style is:

                              if (something)
                              {
                              DoSomething();
                              SoSomethingOther();
                              }
                              else
                              {
                              DoTheOpposite();
                              }

                              As with every style or methodology I use, I try to have a reason for using it, so that I can justify it (even if only to myself!) and, importantly, change should a better method come along (using reasoning to define 'better') So: By having the 'if' aligned with the start and end brace, when scanning code it is trivial to visually see the structure - scan up from the end brace, you just need to look for another brace. If the start brace is at the end of a line of code, then scanning up from an end brace you need to look for if, while, do etc.etc. I always use braces, even with a single line. If, later on, I come back and need to add more cod to the If or the Else, then I insert it between the braces, and never forget - so I don't re-engineer

                              if (a==b)
                              printf("a is equal to b");

                              to

                              if (a==b)
                              printf("a is equal to b");
                              HandleCasesWhereaEqualsb();

                              With the advent of cleverer editors, with automatic indenting, highlighting of code blocks etc., the reasoniong becomes somewhat less important - but you don't get all of that when you open source in or print it (does anyone still print code?) And with the cost of VS2010 in the thousands, I can't guarantee that the editor of my choice will be on every workstation I need to edit on. 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. Much more time is spent looking at code than writing it - often looking for a problem, more often looking for divine intervention or, at least, inspiration!

                              ___________________________________________ .\\axxx (That's an 'M')

                              P J T Z 4 Replies Last reply
                              0
                              • C Colin Rae

                                I much prefer the opening brace on the same line as the statement it belongs to. Just seems nicer to me - I see an if or whatever and look for the corresponding closing brace below it. If I really wanted to show my age, I'd mention the fact that in first year at uni, we used DECWRITER IV hard copy terminals (for you young 'uns, that's basically a printer instead of a monitor)! After doing some course work in the evening, you'd end up going home carrying about half a tree! I missed punched cards by about 2 years - but my older brother used them!

                                L Offline
                                L Offline
                                Lost User
                                wrote on last edited by
                                #19

                                Colin Rae wrote:

                                I missed punched cards by about 2 years

                                Baby! I was a guru on the Holorith manual punch card machine! Hard copy terminals? We dreamed of hard copy terminals (first one we had was a T.I. one with thermal paper which, being a smoker back in the day, used to get really annoying when browsing a listing with a fag (English!) in my hand! And re the braces - see my post elsewhere, but your case only deals with finding the if then searching for the end brace - what about finding the end brace and searching up for the - oh, could be an If or some sort of loop, start of a class, method etc. etc.

                                ___________________________________________ .\\axxx (That's an 'M')

                                C 1 Reply Last reply
                                0
                                • M Mycroft Holmes

                                  Steve Mayfield wrote:

                                  I use an indent with 2 spaces per level

                                  First thing I do when a new dev comes on board, change ALL your indents to 2 spaces. There is nothing worse than picking up some code with a mixture of indents.

                                  Never underestimate the power of human stupidity RAH

                                  L Offline
                                  L Offline
                                  Lost User
                                  wrote on last edited by
                                  #20

                                  Use tabs - then you can set them to whatever indentation you (each) like!

                                  ___________________________________________ .\\axxx (That's an 'M')

                                  M P 2 Replies Last reply
                                  0
                                  • S Soulus83

                                    Code block is so long I can't see the voting bar! This is my preferred style:

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

                                    Each curly brace on it's own line, don't know why but it helps me read the code :confused:

                                    H Offline
                                    H Offline
                                    HimanshuJoshi
                                    wrote on last edited by
                                    #21

                                    Mine too. :thumbsup:

                                    1 Reply Last reply
                                    0
                                    • L Lost User

                                      Colin Rae wrote:

                                      I missed punched cards by about 2 years

                                      Baby! I was a guru on the Holorith manual punch card machine! Hard copy terminals? We dreamed of hard copy terminals (first one we had was a T.I. one with thermal paper which, being a smoker back in the day, used to get really annoying when browsing a listing with a fag (English!) in my hand! And re the braces - see my post elsewhere, but your case only deals with finding the if then searching for the end brace - what about finding the end brace and searching up for the - oh, could be an If or some sort of loop, start of a class, method etc. etc.

                                      ___________________________________________ .\\axxx (That's an 'M')

                                      C Offline
                                      C Offline
                                      Colin Rae
                                      wrote on last edited by
                                      #22

                                      If you've indented it properly, finding the line with the opening brace is trivial!

                                      L 1 Reply Last reply
                                      0
                                      • L Lost User

                                        Use tabs - then you can set them to whatever indentation you (each) like!

                                        ___________________________________________ .\\axxx (That's an 'M')

                                        M Offline
                                        M Offline
                                        Mycroft Holmes
                                        wrote on last edited by
                                        #23

                                        _Maxxx_ wrote:

                                        Use tabs

                                        Sure but in most apps you can set the number of spaces a tab represents, VS is 4, SQL Server is 8 :wtf:, this what I set to 2

                                        Never underestimate the power of human stupidity RAH

                                        1 Reply Last reply
                                        0
                                        • C Colin Rae

                                          If you've indented it properly, finding the line with the opening brace is trivial!

                                          L Offline
                                          L Offline
                                          Lost User
                                          wrote on last edited by
                                          #24

                                          Colin Rae wrote:

                                          If you've indented it properly

                                          If - well sure, but if you indented everything properly then you wouldn't need to have braces at all. you've - not necessarily me who wrote the original trivial - not sure I agree. if your indentation's large enough and your code block small enough, that's usually the case - but scanning upward your eye is still looking for 'something' in that column - rather than a brace in that column. It's no biggie either way - but I find the reasoning behind a brace on a new line to cover more cases than a brace on the end of a line.

                                          ___________________________________________ .\\axxx (That's an 'M')

                                          C P 2 Replies 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