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. Brace style

Brace style

Scheduled Pinned Locked Moved The Lounge
comquestion
140 Posts 69 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 N a v a n e e t h

    Which one you prefer ?

    if{
    //do something
    }

    Or

    if
    {
    //do something
    }


    printf("Navaneeth!!") www.w3hearts.com

    T Offline
    T Offline
    Tom Delany
    wrote on last edited by
    #89

    Navaneeth.Which one you prefer ? if{//do something} Or if{//do something}

    Actually, I prefer: if (condition == true) {     // do something }

    WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated.

    1 Reply Last reply
    0
    • J John R Shaw

      Some years ago I had an application switch over to me so I could fix all the (major) problems with it. By the time I was through all the problems where fixed and all the files where reformatted to my liking. I was still maintaining that code and adding to it till the day I walked out, and it has not been changed since.

      INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

      D Offline
      D Offline
      Dan Neely
      wrote on last edited by
      #90

      if you left how do you know the code never was changed since then?

      -- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer

      J 1 Reply Last reply
      0
      • S Shog9 0

        The latter. The former is used only by miscreants and rogues. :suss:

        ----

        i hope you are feeling sleepy for people not calling you by the same.

        --BarnaKol on abusive words

        C Offline
        C Offline
        cjbauman
        wrote on last edited by
        #91

        Personally, I think it's a question of who really cares as long as you can read the thing and it kinda depends on what language I'm working with. For C# I pretty much go with the second example but as a complete heretic who occasionally finds himself coding Perl in a hurry, I've been known to chuck both examples and go with the following blasphemy: do something if true; Or even (gasp!): do something unless true; Both of which eliminate the braces altogether... :-D

        1 Reply Last reply
        0
        • D Dan Neely

          Wrap it in pre tags to keep the formatting intact.

          -- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer

          T Offline
          T Offline
          Tom Delany
          wrote on last edited by
          #92

          dan neely wrote:

          Wrap it in pre tags to keep the formatting intact.

          :doh: I put &nbsp in for spaces. BTW, it was fun getting the &nbsp to print out!

          WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated.

          1 Reply Last reply
          0
          • C Cyrilix

            I agree with this. if (...) { DoThis(); } ...is just so much more readable when I've got a fairly long and complicated function. I also really despise this: if (...) { DoThis(); } else if { DoThat(); } else { DoThose(); } Extend the DoThis(), DoThat(), and DoThose() to something long, and you'll be going "huh? where the hell are my opening and closing braces?"

            J Offline
            J Offline
            John R Shaw
            wrote on last edited by
            #93

            That speaks louder than anything I could say on the subject. I have actually seen that done and was quick to untie the knots so I could understand what it was supposed to be doing. :laugh:

            INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

            1 Reply Last reply
            0
            • B Brady Kelly

              I like the latter as well, but I don't feel I am in any position to say it's more 'proper'. I would confine my public opinion of what is proper programming is only to ensure all braces match.

              M Offline
              M Offline
              Muhadeeb99
              wrote on last edited by
              #94

              As far as "proper" I sorta meant it as a tongue in cheek remark. The style to which one uses brace formatting is really up to the user. It has its merits either way.

              All things being equal, tommorrow will never equal today

              J 1 Reply Last reply
              0
              • J John R Shaw

                If you keep the lines short then the former works just fine (experience), but the longer the lines get the more likely it is that you will fail to notice the opening brace. Modern environments allow you to place the cursor at a brace and use a key combination to find the other one, but you should not need to do that. If you failed to place the brace after the test statement then you may be confused as to why the compiler generated and error until you search for the missing brace, provided you realize that is the problem. Indention tells you it is supposed to be a block but the compiler does not recognize your intensions, only the facts. When you look at a test statement with an opening brace following it on the next line, you know that it is being blocked out. Of course symmetry also helps when reading the code. I resonantly spent a lot of time programming in ‘.Net’ and the default code generation produced very long lines and it is even hard to stay away from them in standard C++. Which means that unless you start wrapping your test statements to the next line (which I do and hate doing), then the opening brace may not be visible (if you use the former style). If you look at it from a strictly indention point of view then the brace would be in the wrong place because it would be inconsistent with the rest of the code. What all that bull amounts to is that the latter makes it easier to read and debug the code, and I have done enough of both to drive myself a little crazy.

                INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

                T Offline
                T Offline
                Tom Delany
                wrote on last edited by
                #95

                I whole heartedly agree with you. I personally feel that code like

                if (something == true)
                {
                // blah blah
                // blah blah blah
                do
                {
                // blabbity blab
                // ditto
                if (somthingelse == false)
                {
                // initialize the flux capacitor
                }
                } while (somecodition == true);
                }

                makes the blocks of code more clear. Just my personal preference. ;)

                WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated.

                1 Reply Last reply
                0
                • P Pete OHanlon

                  You said it about a lot of contractors there - no-brainer.:laugh: Mind you, I must admit that I cheat - I let the IDE take care of it for me. BTW - my personal preference is braces on a new line.

                  Deja View - the feeling that you've seen this post before.

                  N Offline
                  N Offline
                  NormDroid
                  wrote on last edited by
                  #96

                  Pete O`Hanlon wrote:

                  BTW - my personal preference is braces on a new line

                  We agree on something then;)

                  .net is a box of never ending treasures, every day I get find another gem.

                  D 1 Reply Last reply
                  0
                  • R Rama Krishna Vavilala

                    I have never written anything significant in Perl.

                    K Offline
                    K Offline
                    Kevin McFarlane
                    wrote on last edited by
                    #97

                    You're not missing anything. Dreadful language.

                    Kevin

                    1 Reply Last reply
                    0
                    • D Dan Neely

                      if you left how do you know the code never was changed since then?

                      -- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer

                      J Offline
                      J Offline
                      John R Shaw
                      wrote on last edited by
                      #98

                      Because I lied, there were some minor changes. I know because I was contracted to make them. One of the reasons I left was because they did not really need a full time coder any more, they just did not know it. They did not hire anyone to take my place and any time there is a question or they need something modified they call me. About the only thing that took more than a couple of hours to do was when a third party control failed to work on XP and I had to write a replacement from scratch (the 3rd party did not exist any more). Now if only they would contract me to produce a modern version of the two main programs I would be a happy camper, because they would not let me do it when I worked there.

                      INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

                      1 Reply Last reply
                      0
                      • N N a v a n e e t h

                        Which one you prefer ?

                        if{
                        //do something
                        }

                        Or

                        if
                        {
                        //do something
                        }


                        printf("Navaneeth!!") www.w3hearts.com

                        E Offline
                        E Offline
                        El Corazon
                        wrote on last edited by
                        #99

                        http://astyle.sourceforge.net/[^] because I prefer: style=ansi indent-namespaces and one of the senior programmers keeps us from having a style guide by demanding style=ansi indent-brackets Of course there is programmer brawl number #2342: how do you make a header comment (I was told yesterday there is one and only one way). :)

                        _________________________ Asu no koto o ieba, tenjo de nezumi ga warau. Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)

                        1 Reply Last reply
                        0
                        • N N a v a n e e t h

                          Which one you prefer ?

                          if{
                          //do something
                          }

                          Or

                          if
                          {
                          //do something
                          }


                          printf("Navaneeth!!") www.w3hearts.com

                          G Offline
                          G Offline
                          grgran
                          wrote on last edited by
                          #100

                          God of course intended that there be only one bracing style. All other expose one as a programmer who has fallen from the path. The pattern for an 'if' is as follows:

                          if (conditional) { // waste not, want not: don't waste vertical space
                          statement(s) // note the indention, this clearly identifies these statements as
                          // belonging to the if
                          } // note the indention again, this closes the if logic, it is the
                          // end of the existing block, not the beginning of a new one

                          Of course heritics abound, but my place in "code vault store" is assured (ya have to be a fan of STTNG Klingon afterlife to get that one). Go forth and spread the glad tiding and good bracings and do not hesitate to cntl-K cntl-D the code of the unbelievers. ;):laugh: Brace Zealot -- modified at 11:16 Thursday 17th May, 2007

                          D C 2 Replies Last reply
                          0
                          • N N a v a n e e t h

                            Which one you prefer ?

                            if{
                            //do something
                            }

                            Or

                            if
                            {
                            //do something
                            }


                            printf("Navaneeth!!") www.w3hearts.com

                            E Offline
                            E Offline
                            Eric Georgiades
                            wrote on last edited by
                            #101

                            if
                            {
                            //lalala
                            //doing_stuff
                            }

                            old fashioned i guess

                            Ericos Georgiades

                            1 Reply Last reply
                            0
                            • G grgran

                              God of course intended that there be only one bracing style. All other expose one as a programmer who has fallen from the path. The pattern for an 'if' is as follows:

                              if (conditional) { // waste not, want not: don't waste vertical space
                              statement(s) // note the indention, this clearly identifies these statements as
                              // belonging to the if
                              } // note the indention again, this closes the if logic, it is the
                              // end of the existing block, not the beginning of a new one

                              Of course heritics abound, but my place in "code vault store" is assured (ya have to be a fan of STTNG Klingon afterlife to get that one). Go forth and spread the glad tiding and good bracings and do not hesitate to cntl-K cntl-D the code of the unbelievers. ;):laugh: Brace Zealot -- modified at 11:16 Thursday 17th May, 2007

                              D Offline
                              D Offline
                              Dan Neely
                              wrote on last edited by
                              #102

                              grgran wrote:

                              Of course heritics abound, but my place in "code vault store"

                              Brandishes steel cored foam cluebat. *BIFFF* *BIFFF* *BIFFF* *BIFFF* *BIFFF* *BIFFF* *BIFFF* *BIFFF*

                              -- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer

                              G 1 Reply Last reply
                              0
                              • N NormDroid

                                Pete O`Hanlon wrote:

                                BTW - my personal preference is braces on a new line

                                We agree on something then;)

                                .net is a box of never ending treasures, every day I get find another gem.

                                D Offline
                                D Offline
                                deltalmg
                                wrote on last edited by
                                #103

                                I'm even more anal about bracing. I use if(blah) { stuff even if only one line. Notice the indent; } That way I can line up the if, else etc and eyeball for closing braces to see which block the code is in. I find: if(blah) { dgf dfd df df fdf dfdf } else { dfd df df dsf dg } method annoying. You are forced to look at the first character of each line to see if it is the brace your looking for, rather than scan down the column where the brace your looking for should be.

                                D 1 Reply Last reply
                                0
                                • D deltalmg

                                  I'm even more anal about bracing. I use if(blah) { stuff even if only one line. Notice the indent; } That way I can line up the if, else etc and eyeball for closing braces to see which block the code is in. I find: if(blah) { dgf dfd df df fdf dfdf } else { dfd df df dsf dg } method annoying. You are forced to look at the first character of each line to see if it is the brace your looking for, rather than scan down the column where the brace your looking for should be.

                                  D Offline
                                  D Offline
                                  deltalmg
                                  wrote on last edited by
                                  #104

                                  Oh crap, I got slaughtered by white space removal. The buggers ;) Corrected if(blah) { stuff even if one line. Notice the indent. }

                                  D 1 Reply Last reply
                                  0
                                  • D deltalmg

                                    Oh crap, I got slaughtered by white space removal. The buggers ;) Corrected if(blah) { stuff even if one line. Notice the indent. }

                                    D Offline
                                    D Offline
                                    deltalmg
                                    wrote on last edited by
                                    #105

                                    Thats crazy, even marking it as code this site doesn't keep your formating. Whoever coded this webform is going to go to scripting hell. Every thing he codes will be parsed and emulated on a Comandor 64, no native code for you:laugh:

                                    D 1 Reply Last reply
                                    0
                                    • C Craig Atwood

                                      This is interesting, I myself used the latter, but in the programming course Ive done they teach using the former, although Ive gotten used to programming like that i still dont like it and prefer having the extra line breaks

                                      D Offline
                                      D Offline
                                      DavidGB
                                      wrote on last edited by
                                      #106

                                      Yes, this IS interesting. It weems we were all taught the K&R style, yet aklmost all of us (myself included) have come to prefer the "latter" style. Should we be asking for another revision of K&R? Would anybody pay any attention to it anyway? David

                                      1 Reply Last reply
                                      0
                                      • N N a v a n e e t h

                                        Which one you prefer ?

                                        if{
                                        //do something
                                        }

                                        Or

                                        if
                                        {
                                        //do something
                                        }


                                        printf("Navaneeth!!") www.w3hearts.com

                                        P Offline
                                        P Offline
                                        patbob
                                        wrote on last edited by
                                        #107

                                        Prefer? My preference is irrelevant. The ancients said it best.. when in Rome, do as the Romans. When in someone else's code, match their style. Unless you're writing C#, in which case, Caesar Gates's cronies have decreed how we will all do our braces, spacing and indentation. Fine, whatever, we've all got more important things to do than to fight it anyway.. right? After 20 or so years of reading all different brace styles, you'll pretty much cease to see them anyway.. as long as it is consistent throughout a file. Remember, Rome-Romans. What should you do in completely new files? Sure, fine, whatever. Which do I do in new files? The second one. Why? Because I was asked to. Why was I asked to? I dunno, probably because someone had a harder time of understanding the code with the first one. Why do I bother to comply? Because they claim it helps them.

                                        patbob

                                        1 Reply Last reply
                                        0
                                        • N N a v a n e e t h

                                          Which one you prefer ?

                                          if{
                                          //do something
                                          }

                                          Or

                                          if
                                          {
                                          //do something
                                          }


                                          printf("Navaneeth!!") www.w3hearts.com

                                          M Offline
                                          M Offline
                                          M i s t e r L i s t e r
                                          wrote on last edited by
                                          #108

                                          How about letting the development environment decide based on who you are... that way if you like it one way and your colleague likes it formatted another way, you both can have your cake and eat it too.

                                          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