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.
  • 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
                • L Lost User

                  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 Offline
                  C Offline
                  Colin Rae
                  wrote on last edited by
                  #25

                  _Maxxx_ wrote:

                  If - well sure, but if you indented everything properly then you wouldn't need to have braces at all.

                  Damn compiler seems to want them for some reason...!

                  _Maxxx_ wrote:

                  scanning upward your eye is still looking for 'something' in that column

                  When I've got multiple nesting of braces it doesn't matter whether I'm looking for a brace or an "if" - it's still hard to find the one that lines up! You're right though. It's all personal taste and what you're used to. The one thing I probably hate more than anything is someone trying to impose their own standard as gospel. If I modify someone else's code, I use their style (though I may wince a little!).

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

                    I Offline
                    I Offline
                    Indivara
                    wrote on last edited by
                    #26

                    Either

                    if (condition)
                    {
                    statement;
                    statement;
                    }
                    else
                    {
                    statement;
                    statement;
                    }

                    or

                    if (condition) {
                    statement;
                    statement;
                    } else {
                    statement;
                    statement;
                    }

                    depending what I'm working on, and what everyone else uses in the project. Note the space after the if, and operators will have spaces on either side (like if (a > b)) Your method, I'd only use at gunpoint (and maybe not even then). Well, you asked! ;)

                    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
                      Maximilien
                      wrote on last edited by
                      #27

                      Scarlett Johansson

                      Watched code never compiles.

                      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

                        H Offline
                        H Offline
                        Henry Minute
                        wrote on last edited by
                        #28

                        Whilst I agree with Steve, I wouldn't put myself in the same bracket.

                        Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.” I wouldn't let CG touch my Abacus!

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

                          P Offline
                          P Offline
                          Phil Martin
                          wrote on last edited by
                          #29

                          It's not even close to on topic, but I think the whole premise is flawed. Storing code as plain text is such a pain in the sphincter. Having the code stored in a semantic way and retrieved, displayed and edited in a form that suits me at the time would be vastly preferable to me. But the chances of that happening in my life time? Slim to none. People have stopped talking to each other other spaces vs tabs, let alone not storing code as text.

                          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

                            K Offline
                            K Offline
                            Kasson
                            wrote on last edited by
                            #30

                            :laugh:

                            With Regards, Kasson. Birth is a mistake you'll spend your whole life trying to correct.

                            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

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

                              Steve Mayfield wrote:

                              2 spaces per level

                              But that depends on the font size and resolution -- measure your indent with a ruler, it may be the same visual depth as my four-SPACE indent.

                              1 Reply Last reply
                              0
                              • L Lost User

                                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')

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

                                _Maxxx_ wrote:

                                you wouldn't need to have braces at all.

                                Ah, Python.

                                1 Reply Last reply
                                0
                                • L Lost User

                                  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 Offline
                                  P Offline
                                  PIEBALDconsult
                                  wrote on last edited by
                                  #33

                                  10!

                                  _Maxxx_ wrote:

                                  does anyone still print code?)

                                  Yes, but no so much anymore. I now print with Word -- half-inch margins and 8-point font, I get 112 characters per line. I need braces and such to stand out.

                                  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')

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

                                    _Maxxx_ wrote:

                                    whatever indentation you (each) like

                                    Edit and Notepad insist on 8. X| And, even the otherwise fine Xacc.IDE doesn't adjust indents properly. (When last I tried.)

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

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

                                      PIEBALD's general rule for code formatting (yes, there is one): Things that go together (e.g. parentheses, braces, brackets, etc.) should line up, either horizontally or vertically. Addendum: Separators in lists "go together" with the list designators. Ad nauseum: Be liberal in defining what qualifies as a "separator". :cool: If what you have fits horizontally, so be it, but things are likely to get more complex as they develop so you should plan ahead to avoid having to reformat later. Worrying about vertical space is for others, use as much as you like. Don't choose a style simply because others deride yours; they can reformat if they like.

                                      1 Reply Last reply
                                      0
                                      • P PIEBALDconsult

                                        _Maxxx_ wrote:

                                        whatever indentation you (each) like

                                        Edit and Notepad insist on 8. X| And, even the otherwise fine Xacc.IDE doesn't adjust indents properly. (When last I tried.)

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

                                        If your tool is f*cked it's time to get a new tool.

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

                                        P 1 Reply Last reply
                                        0
                                        • C Colin Rae

                                          _Maxxx_ wrote:

                                          If - well sure, but if you indented everything properly then you wouldn't need to have braces at all.

                                          Damn compiler seems to want them for some reason...!

                                          _Maxxx_ wrote:

                                          scanning upward your eye is still looking for 'something' in that column

                                          When I've got multiple nesting of braces it doesn't matter whether I'm looking for a brace or an "if" - it's still hard to find the one that lines up! You're right though. It's all personal taste and what you're used to. The one thing I probably hate more than anything is someone trying to impose their own standard as gospel. If I modify someone else's code, I use their style (though I may wince a little!).

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

                                          Colin Rae wrote:

                                          If I modify someone else's code, I use their style

                                          Me too - though I once had a contractor working for me who seemed a little slow; it turned out that he didn't like the way we formatted code (this was Delphi code) so spent half of his time re-formatting it rather than doing anything useful. This all wouldn't have been quite so bad if it wasn't for the fact that he didn't like comments.. "People never maintain them, so they're worthless" was his reasoning. So whenever he came across any comment, he removed it!! I fired him.

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

                                          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