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

Curly braces

Scheduled Pinned Locked Moved The Lounge
hostingcloud
43 Posts 25 Posters 58 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.
  • J Jacquers

    Depends on the language. C#: Method() { } Javascrip / Typescript: function() { }

    F Offline
    F Offline
    fgs1963
    wrote on last edited by
    #19

    I have no opinion about braces one way or the other but I have to ask - Why use different styles in different languages? Given it's a choice it seems your personal sense of style would prefer one style all the time. No?

    J 1 Reply Last reply
    0
    • F fgs1963

      I have no opinion about braces one way or the other but I have to ask - Why use different styles in different languages? Given it's a choice it seems your personal sense of style would prefer one style all the time. No?

      J Offline
      J Offline
      Jacquers
      wrote on last edited by
      #20

      I generally just use what's in the specific language's style guide. It makes it easier when different developers work on a project if we stick to those.

      T 1 Reply Last reply
      0
      • Mircea NeacsuM Mircea Neacsu

        Come on!! What's next? Tabs vs spaces?

        Mircea

        M Offline
        M Offline
        MarkTJohnson
        wrote on last edited by
        #21

        A POX upon Tabs.

        I’ve given up trying to be calm. However, I am open to feeling slightly less agitated.

        Mircea NeacsuM D 2 Replies Last reply
        0
        • pkfoxP pkfox

          Are you in this camp

          void AFunc(){
          }

          or this one

          void AFunc()
          {
          }

          I'm in the second

          Life should not be a journey to the grave with the intention of arriving safely in a pretty and well-preserved body, but rather to skid in broadside in a cloud of smoke, thoroughly used up, totally worn out, and loudly proclaiming “Wow! What a Ride!" - Hunter S Thompson - RIP

          S Offline
          S Offline
          Single Step Debugger
          wrote on last edited by
          #22

          I'm in this camp

          void AFunc()
          {
          }

          And for short/inline statements in this one:

          void AFunc() {}

          I never mix them.

          There is only one Vera Farmiga and Salma Hayek is her prophet! Advertise here – minimum three posts per day are guaranteed.

          1 Reply Last reply
          0
          • D den2k88

            I used to be in the first camp, I had to adapt to MISRA rules and now I prefer the second. It's way easier to find missing braces, move braced code and reindent it, even with powerful syntax highlighting - which is not a given in most IDEs for embedded development.

            GCS/GE d--(d) s-/+ a C+++ U+++ P-- L+@ E-- W+++ N+ o+ K- w+++ O? M-- V? PS+ PE Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X

            N Offline
            N Offline
            Nelek
            wrote on last edited by
            #23

            exactly.

            M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpful answers is nice, but saying thanks can be even nicer.

            1 Reply Last reply
            0
            • pkfoxP pkfox

              Are you in this camp

              void AFunc(){
              }

              or this one

              void AFunc()
              {
              }

              I'm in the second

              Life should not be a journey to the grave with the intention of arriving safely in a pretty and well-preserved body, but rather to skid in broadside in a cloud of smoke, thoroughly used up, totally worn out, and loudly proclaiming “Wow! What a Ride!" - Hunter S Thompson - RIP

              N Offline
              N Offline
              Nelek
              wrote on last edited by
              #24

              In the second one, even when an If has only one command to be executed.

              M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpful answers is nice, but saying thanks can be even nicer.

              1 Reply Last reply
              0
              • pkfoxP pkfox

                Are you in this camp

                void AFunc(){
                }

                or this one

                void AFunc()
                {
                }

                I'm in the second

                Life should not be a journey to the grave with the intention of arriving safely in a pretty and well-preserved body, but rather to skid in broadside in a cloud of smoke, thoroughly used up, totally worn out, and loudly proclaiming “Wow! What a Ride!" - Hunter S Thompson - RIP

                G Offline
                G Offline
                godfetish
                wrote on last edited by
                #25

                The only sane format is:

                void Afunc()
                {
                }

                Why #1: Because half the non-curly scoped languages out there use words or other symbols for marking scope, and BEGIN just doesn't look right at the end of the line! Why #2: Java version 1.1 (the first language I used and learned that trailed the curly brace in books) was so horrible, it gave me one more thing to hate! Why #3: Other than using some automated reformatting that kicks lines to the right or left, I find it very difficult to find missing curly braces in some logic. Matched ones line up very clearly. If they trail the line, when things get a few indents deep, the code becomes hard to read and finding that one line you inserted outside of them takes more time than just begin clear the first time. So, always on its own line!

                The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.

                K T 2 Replies Last reply
                0
                • M MarkTJohnson

                  A POX upon Tabs.

                  I’ve given up trying to be calm. However, I am open to feeling slightly less agitated.

                  Mircea NeacsuM Offline
                  Mircea NeacsuM Offline
                  Mircea Neacsu
                  wrote on last edited by
                  #26

                  Please don't go there! One unwinnable debate is enough :) We all have our ways of setting braces and indenting which we know to be right and we'll quash the unbelievers who dare to have a different opinion. We've done that since the time of the first crusade :D

                  Mircea

                  1 Reply Last reply
                  0
                  • J Jacquers

                    I generally just use what's in the specific language's style guide. It makes it easier when different developers work on a project if we stick to those.

                    T Offline
                    T Offline
                    trønderen
                    wrote on last edited by
                    #27

                    Jacquers wrote:

                    I generally just use what's in the specific language's style guide.

                    For C/C++ there is about 42 different bracing / indentation styles fighting for dominance, so referring to "the specific language's" style, as if it was unambiguous, is meaningless. From a logical point, I would prefer

                    void AFunc() {
                    }

                    but I have never seen that promoted in any style guide. The opening brace comes when the statement cannot be completed on the current line, indicating that a block is to follow. You should before you leave the line that you won't find a single one-statement line, but a block. I am one who think "if (day==sunday) weekend = true;" in one line is perfectly fine. "if (day==sunday) {" shows that the statement is not complete. The following block is indented. An indentation always follows a brace. No an indentation without an opening brace; no opening brace without an indentation.

                    "if (day==sunday)
                    {
                    }

                    starts the indentation before the brace - that is inconsistent! Undenting follows a closing brace. No undentation without a closing brace, no closing brace without an undentation.

                    "if (day==sunday)
                    {
                    // indented code block
                    }

                    undents before reaching the closing brace - that is inconsistent. Noone seems to agree with my logic. So I bow my head and follow whatever style guide is enforced upon me. Switch statements mess up indentation in C/C++. One common layout is:

                    switch (day) {
                    case (day == saturday):
                    case (day == sunday):
                    celebrate();
                    break;
                    default:
                    gotowork();
                    break;
                    }

                    The case alternatives are not blocks, so they don't need braces (that is also why they need the 'break'!), but they are indented! I certainly wish they were blocks, for consistency's sake (and I really dislike the 'default is fall through'), so I prefer to make them blocks, adding braces, to justify the indentation. Very few agree with this logic, too - they are so used to seeing indents without any braces justifying it that the inconsistency doesn't bother them.

                    H 1 Reply Last reply
                    0
                    • M MarkTJohnson

                      A POX upon Tabs.

                      I’ve given up trying to be calm. However, I am open to feeling slightly less agitated.

                      D Offline
                      D Offline
                      den2k88
                      wrote on last edited by
                      #28

                      I f-f-fart in the general direction of spaces. I had to work on code where some dev used a single space, some two, some 3 and some idiot 8. As a result it was completely unindented. With tabs there wouldn't have been any issue.

                      GCS/GE d--(d) s-/+ a C+++ U+++ P-- L+@ E-- W+++ N+ o+ K- w+++ O? M-- V? PS+ PE Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X

                      1 Reply Last reply
                      0
                      • pkfoxP pkfox

                        Are you in this camp

                        void AFunc(){
                        }

                        or this one

                        void AFunc()
                        {
                        }

                        I'm in the second

                        Life should not be a journey to the grave with the intention of arriving safely in a pretty and well-preserved body, but rather to skid in broadside in a cloud of smoke, thoroughly used up, totally worn out, and loudly proclaiming “Wow! What a Ride!" - Hunter S Thompson - RIP

                        A Offline
                        A Offline
                        Amarnath S
                        wrote on last edited by
                        #29

                        The compiler doesn't care. Neither do I.

                        D S 2 Replies Last reply
                        0
                        • A Amarnath S

                          The compiler doesn't care. Neither do I.

                          D Offline
                          D Offline
                          den2k88
                          wrote on last edited by
                          #30

                          You're not writing for the compiler.

                          GCS/GE d--(d) s-/+ a C+++ U+++ P-- L+@ E-- W+++ N+ o+ K- w+++ O? M-- V? PS+ PE Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X

                          T 1 Reply Last reply
                          0
                          • A Amarnath S

                            The compiler doesn't care. Neither do I.

                            S Offline
                            S Offline
                            Single Step Debugger
                            wrote on last edited by
                            #31

                            Well, the compiler doesn't care if you use hungarian notation, but this is how office wars start.

                            There is only one Vera Farmiga and Salma Hayek is her prophet! Advertise here – minimum three posts per day are guaranteed.

                            1 Reply Last reply
                            0
                            • G godfetish

                              The only sane format is:

                              void Afunc()
                              {
                              }

                              Why #1: Because half the non-curly scoped languages out there use words or other symbols for marking scope, and BEGIN just doesn't look right at the end of the line! Why #2: Java version 1.1 (the first language I used and learned that trailed the curly brace in books) was so horrible, it gave me one more thing to hate! Why #3: Other than using some automated reformatting that kicks lines to the right or left, I find it very difficult to find missing curly braces in some logic. Matched ones line up very clearly. If they trail the line, when things get a few indents deep, the code becomes hard to read and finding that one line you inserted outside of them takes more time than just begin clear the first time. So, always on its own line!

                              The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.

                              K Offline
                              K Offline
                              k5054
                              wrote on last edited by
                              #32

                              I'd add that a number of us were influenced by K&R C, where you had

                              int foo(str, y)
                              char *str;
                              int y;
                              {
                              /* code */
                              }

                              So an opening curly bracket at the beginning of a function made sense. But then we have:

                              if(a == b ){
                              /* code */
                              }

                              ditto for while, for, etc. Yes, it is inconsistent between functions and every other compound block of code, but blessed by Kernighan and Ritchie, so who am I to argue. So maybe that's Why #4.

                              Keep Calm and Carry On

                              1 Reply Last reply
                              0
                              • G godfetish

                                The only sane format is:

                                void Afunc()
                                {
                                }

                                Why #1: Because half the non-curly scoped languages out there use words or other symbols for marking scope, and BEGIN just doesn't look right at the end of the line! Why #2: Java version 1.1 (the first language I used and learned that trailed the curly brace in books) was so horrible, it gave me one more thing to hate! Why #3: Other than using some automated reformatting that kicks lines to the right or left, I find it very difficult to find missing curly braces in some logic. Matched ones line up very clearly. If they trail the line, when things get a few indents deep, the code becomes hard to read and finding that one line you inserted outside of them takes more time than just begin clear the first time. So, always on its own line!

                                The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.

                                T Offline
                                T Offline
                                trønderen
                                wrote on last edited by
                                #33

                                godfetish wrote:

                                BEGIN just doesn't look right at the end of the line!

                                I really love 'It just ain't done!' arguments! BEGIN is perfectly fine at the end of the line. In my Pascal days, that was the established standard. Note that Pascal switch statements - CASE value OF - uses the OF keyword to start the case list. I never saw anyone put OF on a separate line. It is matched by an END, just like BEGIN. So why would there be a difference between BEGIN ... END and CASE OF ... END? Or would you write Pascal code as

                                CASE i
                                OF
                                0 : Write('zero');
                                1 : Write('one');
                                2 : Write('two');
                                3,4,5,6,7,8,9,10: Write('?')
                                END;

                                Note that if you put BEGIN and OF at separate lines, they are still at the end of the line :-)

                                1 Reply Last reply
                                0
                                • D den2k88

                                  You're not writing for the compiler.

                                  GCS/GE d--(d) s-/+ a C+++ U+++ P-- L+@ E-- W+++ N+ o+ K- w+++ O? M-- V? PS+ PE Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X

                                  T Offline
                                  T Offline
                                  trønderen
                                  wrote on last edited by
                                  #34

                                  I usually do. The compiler is only one who really cares, and analyzes my writings thoroughly!

                                  1 Reply Last reply
                                  0
                                  • T trønderen

                                    Jacquers wrote:

                                    I generally just use what's in the specific language's style guide.

                                    For C/C++ there is about 42 different bracing / indentation styles fighting for dominance, so referring to "the specific language's" style, as if it was unambiguous, is meaningless. From a logical point, I would prefer

                                    void AFunc() {
                                    }

                                    but I have never seen that promoted in any style guide. The opening brace comes when the statement cannot be completed on the current line, indicating that a block is to follow. You should before you leave the line that you won't find a single one-statement line, but a block. I am one who think "if (day==sunday) weekend = true;" in one line is perfectly fine. "if (day==sunday) {" shows that the statement is not complete. The following block is indented. An indentation always follows a brace. No an indentation without an opening brace; no opening brace without an indentation.

                                    "if (day==sunday)
                                    {
                                    }

                                    starts the indentation before the brace - that is inconsistent! Undenting follows a closing brace. No undentation without a closing brace, no closing brace without an undentation.

                                    "if (day==sunday)
                                    {
                                    // indented code block
                                    }

                                    undents before reaching the closing brace - that is inconsistent. Noone seems to agree with my logic. So I bow my head and follow whatever style guide is enforced upon me. Switch statements mess up indentation in C/C++. One common layout is:

                                    switch (day) {
                                    case (day == saturday):
                                    case (day == sunday):
                                    celebrate();
                                    break;
                                    default:
                                    gotowork();
                                    break;
                                    }

                                    The case alternatives are not blocks, so they don't need braces (that is also why they need the 'break'!), but they are indented! I certainly wish they were blocks, for consistency's sake (and I really dislike the 'default is fall through'), so I prefer to make them blocks, adding braces, to justify the indentation. Very few agree with this logic, too - they are so used to seeing indents without any braces justifying it that the inconsistency doesn't bother them.

                                    H Offline
                                    H Offline
                                    honey the codewitch
                                    wrote on last edited by
                                    #35

                                    Why not just put blocks in if you prefer it that way? It also makes it so you can declare variables under the case without the compiler yelling at you.

                                    case 1: {
                                    // do work
                                    }
                                    break; // could be inside

                                    To err is human. Fortune favors the monsters.

                                    T 1 Reply Last reply
                                    0
                                    • pkfoxP pkfox

                                      Are you in this camp

                                      void AFunc(){
                                      }

                                      or this one

                                      void AFunc()
                                      {
                                      }

                                      I'm in the second

                                      Life should not be a journey to the grave with the intention of arriving safely in a pretty and well-preserved body, but rather to skid in broadside in a cloud of smoke, thoroughly used up, totally worn out, and loudly proclaiming “Wow! What a Ride!" - Hunter S Thompson - RIP

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

                                      First, always. The single "{" hanging in the air - taken to the extreme becomes: if (...) { .. } else { .. } versus if () { .. } else { .. }

                                      "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

                                      1 Reply Last reply
                                      0
                                      • H honey the codewitch

                                        Why not just put blocks in if you prefer it that way? It also makes it so you can declare variables under the case without the compiler yelling at you.

                                        case 1: {
                                        // do work
                                        }
                                        break; // could be inside

                                        To err is human. Fortune favors the monsters.

                                        T Offline
                                        T Offline
                                        trønderen
                                        wrote on last edited by
                                        #37

                                        Yes, like I said. But I do not undent until after the closing brace, so I would indent that as well (and include the break in the block). Why not just do it? Because you sometimes have co-workers. For some time, I was using an embedded language (CHILL) which provided the loop construct FOR EVER DO - infinite loops are not uncommon in embedded software. In a new job, programming IoT in C, in one of the modules I was responsible for, I added a "#define ever ;;" for being able to program "for (ever) {...". After a few months, this was discovered by another programmer (not working on my module), who immediately searched through the entire repository to reveal ever use of such "programming jokes", changing them all to "while (0)", deleting the #define and adding a rude remark on the edit where he requested that all programmers refrain from such unserious coding practices in the future. In the next scrum, he brought up his discovery, reporting to everybody how he had 'cleaned up' my code. I asked if he would accept "while (true)", but no: The proper way to code an infinite loop is "while (0)", with a zero. In classical C, "true" is a #define symbol, not part of the base language, and should not be used for fundamental things such as infinite loops. I got no support from the team at all. Not even for "while (true)". In a code review with the same team, I was asked why I would "clutter up" the code with such unnecessary braces in switch statements. I had the same remarks when I added braces to justify the indentation in one-line conditional statements, like

                                        if (day == sunday) {
                                        relax();
                                        }

                                        The coding standard did not allow the "relax()" to be put on the same line as the "if", but mandated the indentation. It didn't explicitly forbid the braces I would like to add - but the programming team did, telling me to remove them. That's how it is working in a team. You have to be obedient. Don't try to make code more readable, or consistent, or safer, if it breaks with the unwritten laws of the team.

                                        H 1 Reply Last reply
                                        0
                                        • T trønderen

                                          Yes, like I said. But I do not undent until after the closing brace, so I would indent that as well (and include the break in the block). Why not just do it? Because you sometimes have co-workers. For some time, I was using an embedded language (CHILL) which provided the loop construct FOR EVER DO - infinite loops are not uncommon in embedded software. In a new job, programming IoT in C, in one of the modules I was responsible for, I added a "#define ever ;;" for being able to program "for (ever) {...". After a few months, this was discovered by another programmer (not working on my module), who immediately searched through the entire repository to reveal ever use of such "programming jokes", changing them all to "while (0)", deleting the #define and adding a rude remark on the edit where he requested that all programmers refrain from such unserious coding practices in the future. In the next scrum, he brought up his discovery, reporting to everybody how he had 'cleaned up' my code. I asked if he would accept "while (true)", but no: The proper way to code an infinite loop is "while (0)", with a zero. In classical C, "true" is a #define symbol, not part of the base language, and should not be used for fundamental things such as infinite loops. I got no support from the team at all. Not even for "while (true)". In a code review with the same team, I was asked why I would "clutter up" the code with such unnecessary braces in switch statements. I had the same remarks when I added braces to justify the indentation in one-line conditional statements, like

                                          if (day == sunday) {
                                          relax();
                                          }

                                          The coding standard did not allow the "relax()" to be put on the same line as the "if", but mandated the indentation. It didn't explicitly forbid the braces I would like to add - but the programming team did, telling me to remove them. That's how it is working in a team. You have to be obedient. Don't try to make code more readable, or consistent, or safer, if it breaks with the unwritten laws of the team.

                                          H Offline
                                          H Offline
                                          honey the codewitch
                                          wrote on last edited by
                                          #38

                                          I've been spoiled for years by being in a situation where I set the standards and control the direction on account of my experience. That said, I understand where you are coming from, but I don't envy you your coworkers. It sounds like you don't have enough written coding standards. If you all wanted to increase your productivity and reduce problems, you'd have linting and formatting scripts for this if the project is significant enough in size. Or at least that's what I'd do, as it tends to pay for itself in terms of more legible and consistent code, and shorter code reviews. :-D

                                          To err is human. Fortune favors the monsters.

                                          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