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

    J Offline
    J Offline
    Jaiprakash M Bankolli
    wrote on last edited by
    #22

    I would certainly go with second style as it make readiability more easier

    Regards, Jaiprakash M Bankolli jaiprakash.bankolli@gmail.com My Blog Suggestions for me

    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
      Niall Joubert
      wrote on last edited by
      #23

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

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

        Oh, no. Not another religious war! :omg: Second.

        Kevin

        1 Reply Last reply
        0
        • D Dario Solera

          The real problem with braces is not the style itself, but different styles in the same project, or even file. Most IDEs can reformat the code, but that way there are a few problems with source control... BTW, I prefer this:

          if(true) {
          // Blah
          }

          If you truly believe you need to pick a mobile phone that "says something" about your personality, don't bother. You don't have a personality. A mental illness, maybe - but not a personality. - Charlie Brooker My Blog - My Photos - ScrewTurn Wiki

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

          Dario Solera wrote:

          different styles in the same project, or even file.

          Yeah, that sucks.

          Kevin

          1 Reply Last reply
          0
          • K Kent Sharkey

            I used to work with a guy that strongly argued for: if { //do something } (He felt that "as a container", the braces themselves needed to be indented. There was a survey back on the old DevelopMentor boards when .NET was still young. The result was about 30% #1, 70% #2 and 1 person "#3")

            -------------- TTFN - Kent

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

            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 1 Reply 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[^]

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

              Mark II wrote:

              Perhaps this similarity to VB.Net is really what some people object to?

              Not me. In the C-family languages I've experimented with both styles and still find the second easier to read. But it's not a big deal. In VB and similar I'm quite happy with the corresponding styles there.

              Kevin

              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
                Paul Watson
                wrote on last edited by
                #28

                If Me = "Red" Then
                do something
                end if

                :P

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

                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

                  R Offline
                  R Offline
                  Rama Krishna Vavilala
                  wrote on last edited by
                  #29

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

                  P N 2 Replies Last reply
                  0
                  • P Paul Watson

                    If Me = "Red" Then
                    do something
                    end if

                    :P

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

                    R Offline
                    R Offline
                    Rama Krishna Vavilala
                    wrote on last edited by
                    #30

                    If Me = "Red" Then
                    do something : END IF

                    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.

                      P Offline
                      P Offline
                      Paul Watson
                      wrote on last edited by
                      #31

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

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

                        Bet you didn't know that! 20+ years ago I read an interview with the two and this topic was broached. They both said they were stunned at the attitude of "...if it's good enough for K & R, it's good enough for me" and the thought by some that copying the K & R style of braces made them a good programmer. Of course we are talking about their infamous book, "The C Programming Language." What they also divuldged was that it was not their brace style at all! Their explanation: The publisher discovered that they could save a whole line of space in the book by moving the opening brace to the end of the previous line and the program would still work. They also remarked, and I'm paraphrasing here, "Thank God the publisher didn't know you could write the whole program on one line!" Now you know the rest of the story... Page 2... :-D The last edition of "The C Programming Language" was updated for ANSI C in 1988. It is still in print! :omg:

                        J D 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

                          M Offline
                          M Offline
                          Muammar
                          wrote on last edited by
                          #33

                          if{//do something;}


                          Smile: A curve that can set a lot of things straight! (\ /) (O.o) (><)

                          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

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

                            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 1 Reply Last reply
                            0
                            • R rtalan

                              Bet you didn't know that! 20+ years ago I read an interview with the two and this topic was broached. They both said they were stunned at the attitude of "...if it's good enough for K & R, it's good enough for me" and the thought by some that copying the K & R style of braces made them a good programmer. Of course we are talking about their infamous book, "The C Programming Language." What they also divuldged was that it was not their brace style at all! Their explanation: The publisher discovered that they could save a whole line of space in the book by moving the opening brace to the end of the previous line and the program would still work. They also remarked, and I'm paraphrasing here, "Thank God the publisher didn't know you could write the whole program on one line!" Now you know the rest of the story... Page 2... :-D The last edition of "The C Programming Language" was updated for ANSI C in 1988. It is still in print! :omg:

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

                              :laugh: Yes! For some reason people do not notice that the code as been reformatted for publication. Some times they do not even notice that the author stated that error checking has been removed for brevity.

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