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

    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
                      • J Joe Woodbury

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

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

                        Joe Woodbury wrote:

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

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

                        J G 2 Replies Last reply
                        0
                        • R realJSOP

                          nikunjbhatt84 wrote:

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

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

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

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

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

                          J 1 Reply Last reply
                          0
                          • J jschell

                            Joe Woodbury wrote:

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

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

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

                            Please get a sense of humor.

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

                              Z Offline
                              Z Offline
                              ZanyZapper
                              wrote on last edited by
                              #71

                              _Maxxx_ wrote:

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

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

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

                                K Offline
                                K Offline
                                KP Lee
                                wrote on last edited by
                                #72

                                If I'm coding using VS, I use the third style because it's not worth the fight to get a different style. If I'm using notepad, I like the 2nd style. An anal observation. If this statement prints, it prints a falsehood.

                                nikunjbhatt84 wrote:

                                print "b is less than or equal to a"

                                if a == b evaluates to true the above statement won't be printed.

                                1 Reply Last reply
                                0
                                • R Ravi Bhavnani

                                  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 Offline
                                  G Offline
                                  ghle
                                  wrote on last edited by
                                  #73

                                  :thumbsup:

                                  Gary

                                  1 Reply Last reply
                                  0
                                  • J Jim SS

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

                                    I've tried it, in fact I have home computer set to run that when I type notepad at the command line, but I don't like it very much. At work we have TextPad, but I stick to what I know -- warts and all.

                                    1 Reply Last reply
                                    0
                                    • M Michael Waters

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

                                      int* pInt;
                                      long& rLong;

                                      or

                                      int *pInt;
                                      long &rLong;

                                      , or even

                                      int * pInt;
                                      long & rLong;

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

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

                                      Hear! Hear! When I get my time machine working I'm going back to have a word with Dennis Ritchie. :-D

                                      1 Reply Last reply
                                      0
                                      • J Joe Klemmer

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

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

                                        J 1 Reply Last reply
                                        0
                                        • D Dan Neely

                                          Maybe, but IIRC K&R wrote their manuscript using this style originally:

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

                                          It was changed at the publishers request to save on printing costs.

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

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

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

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

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

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

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

                                          D 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