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 4 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.
  • 1 123 0

    [Message Deleted]

    L Offline
    L Offline
    lotuspro
    wrote on last edited by
    #36

    Doh, I learned to code from that book. For my 2c, I find myself siding daily with Messrs Kernighan and Richie on this point. I can't abide by all those extra line breaks.

    B 1 Reply Last reply
    0
    • N Niall Joubert

      As long as there are braces, there will be debate, some fierce... :) Having been a fan of the later since varsity, I have recently seen the light and converted to being a fan of the former. On the suggestion of a good friend, I experimented with it in a recent project and have seen the error of my ways. I find the former to more clearly show 'blocks' of code, and thus to be far more readable and maintainable. Having just said that, it did take me a week or two to get used to it though. I work on several projects, using both styles. My only real comment is that consistency is more important and that always seems to be the common point people make. Long live OTBS... :)

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

      Niall Joubert wrote:

      As long as there are braces, there will be debate, some fierce...

      True. ;) I can switch between both at will and once thought the former was a good idea. I have since learned the errors of my ways, because the former makes it more difficult to see where the block begins or if there is even a block to begin with. Regardless of the project, I automatically use what ever style is currently used if I am modifying or adding to someone else’s code.

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

      N K 2 Replies Last reply
      0
      • M Mark II

        if (condition) {     //do something } This is the only true way. All else is heresy. ;P Seriously, I think this all just comes down to personal preference. Everyone has his or her own reasons for preferring one style to another. Those reasons could be to do with practicality, ideology, aesthetics, familiarity, or just following standards. In my case, I like the above layout because it takes up fewer lines on the screen, so I can see more of my code at once. I simply can't see why some people think that the alternatives look nicer. Also, my background is in languages that don't have “curlies”, but whose layout is more like this layout. For example, the structure of VB.Net code is similar: If condtion THEN     'do something END IF Perhaps this similarity to VB.Net is really what some people object to? My Blog: http://www.allwrong.com[^]

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

        Actually what you are saying is heresy. If you do not know that then I am not going to explain it you. In any case, simple logic and experience will eventually show you what the problem is. P.S. I used that style one upon a time (for the same reasons).

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

        M 1 Reply Last reply
        0
        • J John R Shaw

          Actually what you are saying is heresy. If you do not know that then I am not going to explain it you. In any case, simple logic and experience will eventually show you what the problem is. P.S. I used that style one upon a time (for the same reasons).

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

          M Offline
          M Offline
          Mark II
          wrote on last edited by
          #39

          John R. Shaw wrote:

          simple logic and experience will eventually show you what the problem is

          I can over-complicate things for anyone. Sadly, simple logic really is not one of my strong points. :( However, I'd be delighted to benefit from experience, and I don't doubt that yours is more extansive than mine. Please, do enlighten me. I would like to learn.

          J 1 Reply Last reply
          0
          • R Rama Krishna Vavilala

            For c++ the later For JavaScript the former For C# either one based on what the convention is for the project.

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

            For Perl?


            Programming Blog utf8-cpp

            R 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

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

              Whatever makes the boss happy. If I am the boss, probably K&R these days.


              Programming Blog utf8-cpp

              R 1 Reply Last reply
              0
              • 1 123 0

                [Message Deleted]

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

                Actually no, it puts both Kernighan and Richie in the category of those trying to save space in published material. In the first document they only mention writing style one time and in that case the example given is the second and not the first. In the second document they do not mention it at all

                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 Nemanja Trifunovic

                  Whatever makes the boss happy. If I am the boss, probably K&R these days.


                  Programming Blog utf8-cpp

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

                  Nemanja Trifunovic wrote:

                  probably K&R these days

                  K&R the dudes or K&R the book? See my post: Even K & R preffered the latter

                  B N 2 Replies Last reply
                  0
                  • K Kevin McFarlane

                    Indented braces are argued for in the otherwise excellent Code Complete book. It was one of my few disagreements with McConnell. I prefer style 2 but am OK reading style 1. I can never get used to indented braces.

                    Kevin

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

                    I have never known anyone who used them. But then again I have known few peaple who seem to want to make their life more difficult.

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

                    D 1 Reply Last reply
                    0
                    • J John R Shaw

                      Niall Joubert wrote:

                      As long as there are braces, there will be debate, some fierce...

                      True. ;) I can switch between both at will and once thought the former was a good idea. I have since learned the errors of my ways, because the former makes it more difficult to see where the block begins or if there is even a block to begin with. Regardless of the project, I automatically use what ever style is currently used if I am modifying or adding to someone else’s code.

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

                      N Offline
                      N Offline
                      Niall Joubert
                      wrote on last edited by
                      #45

                      John R. Shaw wrote:

                      I automatically use what ever style is currently used

                      Dexterity... :laugh: Frankly though, as long as tabbing is consistent (preferably spaces though), either style should provide a relatively clear indication of where the blocks are.

                      J 1 Reply Last reply
                      0
                      • K Kyudos

                        The second form is preferable, I don't know how anyone can put up with the lack of symmetry in the first form. UGH! X| X| X|

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

                        Bingo! :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
                        • S SimonRigby

                          LOL That used to be me. Don't know why but lately I switched to the latter.

                          The only thing unpredictable about me is just how predictable I'm going to be.

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

                          I like the latter, because it conforms to style of proper programming. the boolean in me says yes.

                          All things being equal, tommorrow will never equal today

                          B 1 Reply Last reply
                          0
                          • P Paul Watson

                            Why the different styles dependant on language?

                            regards, Paul Watson Ireland & South Africa

                            Shog9 wrote:

                            And with that, Paul closed his browser, sipped his herbal tea, fixed the flower in his hair, and smiled brightly at the multitude of cute, furry animals flocking around the grassy hillside where he sat coding Ruby on his Mac...

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

                            There seems to be a "house style" for langauges. So Java and JavaScript seems mostly to follow the former. Microsoft-centric code tends to follow the latter.

                            Kevin

                            1 Reply Last reply
                            0
                            • R rtalan

                              Nemanja Trifunovic wrote:

                              probably K&R these days

                              K&R the dudes or K&R the book? See my post: Even K & R preffered the latter

                              B Offline
                              B Offline
                              blackjack2150
                              wrote on last edited by
                              #49

                              I guess the Java language is a taboo subject here. The Java code-style guide explicitly mentions the first form as desirable. And I respect that when coding in Java. However, I feel more comfortably with the second and I use that one in C#.

                              The more I see of men, the better I like my dog. Blaise Pascal.

                              1 Reply Last reply
                              0
                              • M Muhadeeb99

                                I like the latter, because it conforms to style of proper programming. the boolean in me says yes.

                                All things being equal, tommorrow will never equal today

                                B Offline
                                B Offline
                                Brady Kelly
                                wrote on last edited by
                                #50

                                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 1 Reply Last reply
                                0
                                • P Pete OHanlon

                                  Who cares? Does this actually improve the quality of your code in any way? Does the first version produce different code to the second? It doesn't matter. As long as you are consistent and follow internal coding standards then it shouldn't matter which one it is.

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

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

                                  Pete O`Hanlon wrote:

                                  Who cares?

                                  I care, the first leads to unreadable code and working for many software houses, the first think you learn are the programming standard for that company and at a time top of the list if braces style. You'd get hanged if you coded the brace if(blah de blah){ } If so freakin unreadble!

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

                                  K P 2 Replies Last reply
                                  0
                                  • J John R Shaw

                                    Simple the second choice, because it makes more since and is easier to read. I used the first one for a few years and eventually found it very annoying when reading the code, because it is too easy to miss where the block starts. The third option would be to indent the braces, but that has never made since to me. Of course writing ‘if ()’ as opposed to ‘if()’ still does not make since to me, unless you are programming in multiple languages, where one requires that (like VB6). And I would like to know which idiot started the trend of writing ‘i++’ instead of ‘++i’, because that is not a matter of formatting but is a matter of understanding the language. Sorry, I almost got carried away for something so simple.

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

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

                                    John R. Shaw wrote:

                                    Simple the second choice, because it makes more since and is easier to read. I used the first one for a few years and eventually found it very annoying when reading the code, because it is too easy to miss where the block starts.

                                    I fully agree.

                                    John R. Shaw wrote:

                                    he third option would be to indent the braces, but that has never made since to me.

                                    Same here.

                                    John R. Shaw wrote:

                                    Of course writing ‘if ()’ as opposed to ‘if()’ still does not make since to me

                                    It makes perfect sense to me. I hate not having the space after the keyword. However, it's not a big deal. It's less of a deal than bracket placement and that's not much of a deal.

                                    John R. Shaw wrote:

                                    And I would like to know which idiot started the trend of writing ‘i++’ instead of ‘++i’, because that is not a matter of formatting but is a matter of understanding the language.

                                    Most of the time it makes no difference and on most of the occasions when it does we shouldn't be writing code that way anyway. I've got used to writing i++ and it just looks nicer to me. However, I'm clued up enough to use ++it for STL iterators. But I haven't done any C++ for over 2 years.

                                    Kevin

                                    J 1 Reply Last reply
                                    0
                                    • L lotuspro

                                      Doh, I learned to code from that book. For my 2c, I find myself siding daily with Messrs Kernighan and Richie on this point. I can't abide by all those extra line breaks.

                                      B Offline
                                      B Offline
                                      Brady Kelly
                                      wrote on last edited by
                                      #53

                                      Line breaks that do what? Bloat your source? I seldom code methods that need more than my current window, and that is with being anal about braces, as in even for single statement 'if' blocks, e.g. if (someExpression) { SomeStatement; }

                                      R 1 Reply Last reply
                                      0
                                      • P Pete OHanlon

                                        Who cares? Does this actually improve the quality of your code in any way? Does the first version produce different code to the second? It doesn't matter. As long as you are consistent and follow internal coding standards then it shouldn't matter which one it is.

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

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

                                        Pete O'Hanlon wrote:

                                        Does this actually improve the quality of your code in any way?

                                        No, but religious wars are fun.:) Pointless, but fun.

                                        Kevin

                                        N 1 Reply Last reply
                                        0
                                        • N NormDroid

                                          Pete O`Hanlon wrote:

                                          Who cares?

                                          I care, the first leads to unreadable code and working for many software houses, the first think you learn are the programming standard for that company and at a time top of the list if braces style. You'd get hanged if you coded the brace if(blah de blah){ } If so freakin unreadble!

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

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

                                          norm .net wrote:

                                          I care, the first leads to unreadable code

                                          I don't think it's unreadable. But it's less readable. However. many think the first is more readable on the grounds that they can see more lines of code at once. Incidentally there is some variation on the first style if (blah) { } else { } versus if (blah) { } else { } The latter is even worse as the if and else don't line up.

                                          Kevin

                                          N 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