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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Where do you put the braces?

Where do you put the braces?

Scheduled Pinned Locked Moved C / C++ / MFC
questiontutorial
17 Posts 14 Posters 0 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.
  • P Paul M Watt

    The primary reason that you see code written like this in books is to save page space: void foo { // stuff }


    Build a man a fire, and he will be warm for a day
    Light a man on fire, and he will be warm for the rest of his life!

    L Offline
    L Offline
    Link2600
    wrote on last edited by
    #6

    Do you mean you use line up method too? Do you do the following too? class Cat { // codes };

    P 1 Reply Last reply
    0
    • L Link2600

      Do you mean you use line up method too? Do you do the following too? class Cat { // codes };

      P Offline
      P Offline
      Paul M Watt
      wrote on last edited by
      #7

      Yes, I code like this: class Cat { // codes }; I liberally use newlines and I like to organize my braces vertically. I have seen a variety of ways that people organize their code, especially when it comes to the use of braces. I think the bottom line is what is most readable to you and your team of developers that you work with.


      Build a man a fire, and he will be warm for a day
      Light a man on fire, and he will be warm for the rest of his life!

      1 Reply Last reply
      0
      • P Paul M Watt

        The primary reason that you see code written like this in books is to save page space: void foo { // stuff }


        Build a man a fire, and he will be warm for a day
        Light a man on fire, and he will be warm for the rest of his life!

        S Offline
        S Offline
        Shafiee
        wrote on last edited by
        #8

        If it's correct, you may want to declare a class like: Class Cat { Cat(); ~Cat(); //some other code}; I don't think only saving page space is assumed!

        I 1 Reply Last reply
        0
        • L Link2600

          I have seen tons of reference books doing this. They put the braces right after the function names or class names. For example, instead of doing this: class Cat { // codes }; they do this: class Cat { // codes }; But obviously, the first one is much clear than the second one and not likely to make mistakes too. What do you thinks? Especially those real world programmers, what is your habit and why?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #9

          I always do it the first way. It takes up a few more lines, but the matching pairs are much easier to see, much neater. Elaine :rose: The tigress is here :-D

          1 Reply Last reply
          0
          • L Link2600

            I have seen tons of reference books doing this. They put the braces right after the function names or class names. For example, instead of doing this: class Cat { // codes }; they do this: class Cat { // codes }; But obviously, the first one is much clear than the second one and not likely to make mistakes too. What do you thinks? Especially those real world programmers, what is your habit and why?

            M Offline
            M Offline
            markkuk
            wrote on last edited by
            #10

            The One True Way to put the braces is the one used by Kernighan&Ritchie, and all other styles are heretic abominations. ;P

            M 1 Reply Last reply
            0
            • L Link2600

              I have seen tons of reference books doing this. They put the braces right after the function names or class names. For example, instead of doing this: class Cat { // codes }; they do this: class Cat { // codes }; But obviously, the first one is much clear than the second one and not likely to make mistakes too. What do you thinks? Especially those real world programmers, what is your habit and why?

              A Offline
              A Offline
              Anonymous
              wrote on last edited by
              #11

              I do as : class Cat { // codes }; I think this form is better.

              1 Reply Last reply
              0
              • L Link2600

                I have seen tons of reference books doing this. They put the braces right after the function names or class names. For example, instead of doing this: class Cat { // codes }; they do this: class Cat { // codes }; But obviously, the first one is much clear than the second one and not likely to make mistakes too. What do you thinks? Especially those real world programmers, what is your habit and why?

                M Offline
                M Offline
                Michael Dunn
                wrote on last edited by
                #12

                For further reading: Braces and brackets[^] --Mike-- Latest blog entry: *drool* (Alyson) [May 10] Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber "You have Erica on the brain" - Jon Sagara to me

                1 Reply Last reply
                0
                • L Link2600

                  I have seen tons of reference books doing this. They put the braces right after the function names or class names. For example, instead of doing this: class Cat { // codes }; they do this: class Cat { // codes }; But obviously, the first one is much clear than the second one and not likely to make mistakes too. What do you thinks? Especially those real world programmers, what is your habit and why?

                  J Offline
                  J Offline
                  John M Drescher
                  wrote on last edited by
                  #13

                  I have seen many discussions about this subject on this site. You may try a search to see if you can find them. Anyways, I use the first method for classes annd functions but the second for just about everything else. To me it is a matter of saving space. John

                  1 Reply Last reply
                  0
                  • M markkuk

                    The One True Way to put the braces is the one used by Kernighan&Ritchie, and all other styles are heretic abominations. ;P

                    M Offline
                    M Offline
                    Michael P Butler
                    wrote on last edited by
                    #14

                    markkuk wrote: The One True Way to put the braces is the one used by Kernighan&Ritchie, and all other styles are heretic abominations. :-D that's fine for C code but C++ does it properly Michael 'War is at best barbarism...Its glory is all moonshine. It is only those who have neither fired a shot nor heard the shrieks and groans of the wounded who cry aloud for blood, more vengeance, more desolation. War is hell.' - General William Sherman, 1879

                    1 Reply Last reply
                    0
                    • L Link2600

                      I have seen tons of reference books doing this. They put the braces right after the function names or class names. For example, instead of doing this: class Cat { // codes }; they do this: class Cat { // codes }; But obviously, the first one is much clear than the second one and not likely to make mistakes too. What do you thinks? Especially those real world programmers, what is your habit and why?

                      Z Offline
                      Z Offline
                      ZoogieZork
                      wrote on last edited by
                      #15

                      I've used both styles, read code using both styles, debugged code using both styles, and as far as I'm concerned, I have no problem with either form. It's not really brace matching I have problems with -- it's parens (I once spent almost a day trying to debug a parser written in Scheme that boiled down to a missing end paren... paren matching? What's that?) :) - Mike

                      1 Reply Last reply
                      0
                      • S Shafiee

                        If it's correct, you may want to declare a class like: Class Cat { Cat(); ~Cat(); //some other code}; I don't think only saving page space is assumed!

                        I Offline
                        I Offline
                        includeh10
                        wrote on last edited by
                        #16

                        i appreciate your code: save many lines includeh10

                        1 Reply Last reply
                        0
                        • L Link2600

                          I have seen tons of reference books doing this. They put the braces right after the function names or class names. For example, instead of doing this: class Cat { // codes }; they do this: class Cat { // codes }; But obviously, the first one is much clear than the second one and not likely to make mistakes too. What do you thinks? Especially those real world programmers, what is your habit and why?

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

                          The only reasonable reason for formating you code like this class Cat { // codes }; is because the amount of space in published works (a.k.a books) is limited and you need to squeese the code so you can display it in less space. But formating your like this can lead to problems when debuging your code. I used this format some years back and I need to appoligise to anyone who is required to maintain it. Trust in the code Luke. Yea right!

                          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