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

    A Offline
    A Offline
    AdamEcc
    wrote on last edited by
    #42

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

    With the exception of single-statement getters/setters, in which case

    public int SomeProperty
    {
    get{return somePropertyValue;}
    set
    {
    if (value != somePropertyValue)
    {
    someProperty = value;
    // raise value changed event
    }
    }
    }

    I like that putting the open brace under the function / operation name makes it easy to spot where braces match up, but think it looks clunky when you only have a single line in a getter / setter.

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

      R Offline
      R Offline
      R Erasmus
      wrote on last edited by
      #43

      Nothing beats this, I'm sorry ;(

      if (a > b)
      {
      /* Code */
      }
      else if (c == d)
      {
      /* Code */
      }
      else
      {
      /* Code */
      }

      "Program testing can be used to show the presence of bugs, but never to show their absence." << please vote!! >>

      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
        Dale Barnard
        wrote on last edited by
        #44

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

          S Offline
          S Offline
          Stuart Rubin
          wrote on last edited by
          #45

          First of all, I'm happy to read this forum and not see too many "religious" arguments for styling one way or another! There are good, objective reasons to use different styles. I always go back to McConnell's "Code Complete." Chapter 31 is all about layout and style. Basically, the layout style needs to address these. I'm quoting headings from the book: Accurately represent the logical structure of the code Consistently represent the logical structure of the code Improve readability Withstand modification I think the first two are obvious. The latter two are what require the thought. Personally, I'm a white-space-supremacist bracist (good pun, earlier poster!), and like:

          // Check for matching data results
          if(a == b)
          {
          // The data match.
          doSomething();
          }
          else if (a == c)
          {
          // Something else matched
          doSomethingElse();
          }
          else
          {
          // Something may have gone wrong.
          doErrorCode();
          }

          The blocks are easily identifiable. It's almost impossible to screw up when you add code. It's also easy to comment; the comments naturally identify the blocks.

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

            S Offline
            S Offline
            Stefan_Lang
            wrote on last edited by
            #46

            if (you_had_asked_me_10_years_ago)
            {
            I_would_have_done_it = this_way;
            }
            else
            {
            see = below;
            }

            if (you_ask_me_now) {
            that = what_I_prefer_now;
            }
            else {
            what=ever;
            }

            My main reasons are: 1. optical readability through indentation - all the actual code belonging to the block is indented 2. the ending '}' is easier to find when it's not indented with the block 3. there is no reason to start a new line for an opening '{' since the statement causing the new control block already is signal enough, and optically starts the block because of the indentation in the following line(s) 4. I always use curly brackets, even if there's only one line. (a) This way I better recognize that there is some kind of control flow even when the indentation is messed up. (b) Also, in my experience most one-line blocks won't stay one-liners for long. (c) And I also get some consistentency with control commands, as they'll always take one line to start a block and one line (the '}' on a seperate line) to end. Of course, much of this is a matter of taste, but after trying several styles over the past 25 years, this is what pleases me most.

            1 Reply Last reply
            0
            • R Ravi Bhavnani

              Heh.  You'll probably get as many responses as there are developers.  Or religions. :) I prefer the Java style because it allows me to see more code using a shorter display.  But when I'm editing code written by another developer, I adhere to his/her style. /ravi

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

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

              Ravi Bhavnani wrote:

              the Java style

              That would be K&R style :)

              utf8-cpp

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