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:

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

    I, too, align left and right braces under their base keyword. I have done this since I learnt Algol 60 which predates the C family of languages and had begin and end rather than { and }. When I learnt Pascal, which overloaded the end keyword, I added another convention: Add an end-of-construct comment. Labels (e.g. for switch cases) are aligned with the braces and the code that they point to is on another line indented from the brace. Typical fragments (not working code):

    if (discr > 0)
    {
    // Work out real roots
    ...
    } // if
    else if (discr < 0)
    {
    // Work out imaginary roots
    ...
    } // else if
    else // Equal roots
    switch (dayOfWeek)
    {
    case Sat:
    case Sun:
    alert('Equal roots at the weekend - take the day off');
    break;

    case Wed:
    alert('Mid week with equal roots - take a nap');
    break;

    default:
    alert('Do unpaid overtime');
    break;
    } // switch

    No, I do not brace single statement; and yes, I do treat else if as though it were a keyword even though it is not in most languages. Let the flame wars commence. Does anyone know how to insert blank lines in code samples?

    M 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
      Peter_Olson
      wrote on last edited by
      #49

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

        F Offline
        F Offline
        Frank Peelo
        wrote on last edited by
        #50

        it's obvious why. The braces are used to mark the start and end of a block, and you have them here in the same column (so your eye knows where to go to look for it) and without hiding them in other text. This is obviously a good style for this language. If you, or the person who taught you, or (the person who taught)^n you comes from a language where keywords delimit blocks, then you could end up with the style where braces are placed on the same line as the keyword. So if (a>b) ... blah ... else ... blah ... endif becomes if (a>b) { ... blah ... } else { ... blah ... } This is clearly inappropriate behaviour in the latter case because the keywords here do not delimit the block. The braces do, but they are made more difficult to find. It is appropriate in the other language, because the keywords which delimit the block are in the same column and are not hidden in other text.

        1 Reply Last reply
        0
        • L Lost User

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

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

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

          The tool is fine. I just need to use it in accordance with its rules. edlin, on the other hand...

          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:

            S Offline
            S Offline
            spencepk
            wrote on last edited by
            #52

            Same here

            1 Reply Last reply
            0
            • N Nemanja Trifunovic

              Ravi Bhavnani wrote:

              the Java style

              That would be K&R style :)

              utf8-cpp

              R Offline
              R Offline
              Ravi Bhavnani
              wrote on last edited by
              #53

              Yes, exactly!  I still have my circa 1981 copy of K&R! :) /ravi

              My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

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

                D Offline
                D Offline
                dpminusa
                wrote on last edited by
                #54

                Alt 2, since this is what the if snippet produces. I try to use all the snippet templates. As mentioned it can produce auto indenting on some editors, e.g. Komodo. Komodo also has code folding that helps emphasize the scope areas.

                "Courtesy is the product of a mature, disciplined mind ... ridicule is lack of the same - DPM"

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

                  J Offline
                  J Offline
                  Jim SS
                  wrote on last edited by
                  #55

                  Notepad++ has replaced my notepad for all but the simplest views.

                  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 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
                    Jim SS
                    wrote on last edited by
                    #56

                    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 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
                      patbob
                      wrote on last edited by
                      #57

                      Why not just go with whatever Visual Studio does when you use Ctrl-K Ctrl-F to reformat/reindent a block of code?

                      patbob

                      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
                        megatron23
                        wrote on last edited by
                        #58

                        quote:

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

                        I indeed like that style the most. In my opinion it is't unreadable at all as it seperates clearly nested code from the control flow statements. For me, being a Basic 'programmer' in my very young days stigmatized me in being really comfortable with this style of code. As is Basic I find just utilizing indentions much more readable and banish the braces into the flow statements. regards Thomas

                        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:

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

                          For me, this is the easiest way to determine functional blocks. But, for the example of if..else, I seem to vacillate on the else part. Lately I have been using this style -

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

                          But, sometimes, I find myself typing this -

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

                          No idea why.

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

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

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

                            > I always use braces, even with a single line. I'm with you there. The only time I end up having something like -

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

                            Is when I'm copying/using someone elses code and forget to add the braces. Usually due to laziness.

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

                            1 Reply Last reply
                            0
                            • J jsc42

                              I, too, align left and right braces under their base keyword. I have done this since I learnt Algol 60 which predates the C family of languages and had begin and end rather than { and }. When I learnt Pascal, which overloaded the end keyword, I added another convention: Add an end-of-construct comment. Labels (e.g. for switch cases) are aligned with the braces and the code that they point to is on another line indented from the brace. Typical fragments (not working code):

                              if (discr > 0)
                              {
                              // Work out real roots
                              ...
                              } // if
                              else if (discr < 0)
                              {
                              // Work out imaginary roots
                              ...
                              } // else if
                              else // Equal roots
                              switch (dayOfWeek)
                              {
                              case Sat:
                              case Sun:
                              alert('Equal roots at the weekend - take the day off');
                              break;

                              case Wed:
                              alert('Mid week with equal roots - take a nap');
                              break;

                              default:
                              alert('Do unpaid overtime');
                              break;
                              } // switch

                              No, I do not brace single statement; and yes, I do treat else if as though it were a keyword even though it is not in most languages. Let the flame wars commence. Does anyone know how to insert blank lines in code samples?

                              M Offline
                              M Offline
                              Michael Waters
                              wrote on last edited by
                              #61

                              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.

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

                                K Offline
                                K Offline
                                K Lynn
                                wrote on last edited by
                                #62

                                What curly braces? I don't use no curly stinkin' braces! :)

                                1 Reply Last reply
                                0
                                • D Dale Barnard

                                  I've seen so many developers (especially newer ones) obsess over code syntactic readability, but I have found that I can get used to any style. My mental focus is mainly on code design (class, method, static definitions, etc.) more than the syntactic details. I would write try { myField = new Field(...); } catch { report error } because at a design level, it is only one logical statement. The error handing in this case is related to that statement, so I kept it on the same line. If it takes two steps to do something, I'll spread out the code to more lines: try { myField = new Field(...); myField.Event += event handler; } catch { report error }; Still, my focus is not on seeing (or obsessing over) curly braces, but rather on seeing the logical steps going on. I want the curly braces to drift into the background. I'm a contractor, so I match the style used by each employer, and I adapt pretty quickly to whatever they do, no matter how illogical. I don't let myself obsess over syntax, so once I get used to a style, those curly braces just disappear from my view, leaving the logical structure. When code has inconsistent syntax (such as each developer using a unique style), I can't help but notice the syntax, distracting me from higher-level design. Thus, the most important thing for me is CONSISTENCY.

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

                                  > Thus, the most important thing for me is CONSISTENCY. Amen, to that, brother. One thing I find strange is that coding standards aren't as ubiquitous or enforced as they should be. It's like they are suggestions rather than mandatory.

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

                                  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
                                    IdahoEngr
                                    wrote on last edited by
                                    #64

                                    I like to use a compact form with braces in control statements on the same line, but also don't like the shorthand without braces. However, I switch for namespace and class declarations, as well as class member functions. 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" } namespace name { class dummy : public base { public: dummy( void ) { } }; } "Luck is the residue of Design"

                                    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:

                                      T Offline
                                      T Offline
                                      TheRegan
                                      wrote on last edited by
                                      #65

                                      + 1000000 Although I often fight with myself about single lines i.e. whether to:

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

                                      or:

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

                                      I hate switcing between styles (I am a consistency vampire) so I usually go with the latter. Many of the people I work with go with a single line though. I get that it looks neater and makes for less lines of code.. Maybe I'm just being too fussy?

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

                                        T Offline
                                        T Offline
                                        TheRegan
                                        wrote on last edited by
                                        #66

                                        Love it. Excellent explanation.

                                        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
                                          Naruki 0
                                          wrote on last edited by
                                          #67

                                          Known to discerning developers the world over as 1TBS, baby! In my version, the closing brace is always on a line by itself, even before the else statement. This way I can insert/delete/move else if blocks without screwing around too much. Plus, } else { just hurts my sense of aesthetics.

                                          Narf.

                                          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