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. Damn c# { }'s

Damn c# { }'s

Scheduled Pinned Locked Moved The Lounge
csharpquestioncareer
68 Posts 47 Posters 2 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.
  • V V 0

    * I usually mark them with a comment (end if, end for, ...), some find it childish, but I found it often helpful * setup VS to clearly show matching braces * ALWAYS use the {} even if there is only one statement. Most common error for me if someone else wrote something like (cause I never, ever do that):

    if [statement a]
    else if [statement b]
    else if [statement c]
    else if [statement d]
    else if [statement e]
    else if [statement f]
    else [statement g]

    and you need to insert an additional statement in between somewhere. hope this helps.

    V.
    (MQOTD rules and previous solutions)

    R Offline
    R Offline
    RossMW
    wrote on last edited by
    #29

    Never thought of the obvious of commenting the end brace. Doh I have setup the ide to have a brighter highlight but I notice it sometimes does work. Generally if there is some other minor error..:confused:

    1 Reply Last reply
    0
    • Z Zafar Sultan

      I have a habit of typing } followed by back key as soon as I type {. This keeps things under control as the chances to forget typing } are none. I would also suggest to try outlining for visual studio. Try this[^]. Using this you will be able to work in the current block while collapsing the other code blocks. Another thing I would like to suggest is the use of ctrl+K+D(for code formatting), Ctrl+M+O and Ctrl+M+P to toggle outlining. I find these combinations very handy while coding.

      R Offline
      R Offline
      RossMW
      wrote on last edited by
      #30

      The tool looks good. Going to give it a go when I can. Thanks

      Z 1 Reply Last reply
      0
      • R RossMW

        The tool looks good. Going to give it a go when I can. Thanks

        Z Offline
        Z Offline
        Zafar Sultan
        wrote on last edited by
        #31

        You're welcome :)

        1 Reply Last reply
        0
        • S Super Lloyd

          how about... instead of

          if (condition)
          {
          ....
          }

          use

          if (!condition)
          return;
          ....

          All in one Menu-Ribbon Bar DirectX for WinRT/C# since 2013! Taking over the world since 1371!

          J Offline
          J Offline
          JeremyBob
          wrote on last edited by
          #32

          Not just that, but be consistent in your if statements if you don't want braces issue. i.e. ALWAYS use the braces, even in the above !condition case. Personally I find Resharper is an excellent tool to help "flatten" you code by suggesting inverting your if statements.

          if (!condition)
          {
          return;
          }

          S H 2 Replies Last reply
          0
          • J JeremyBob

            Not just that, but be consistent in your if statements if you don't want braces issue. i.e. ALWAYS use the braces, even in the above !condition case. Personally I find Resharper is an excellent tool to help "flatten" you code by suggesting inverting your if statements.

            if (!condition)
            {
            return;
            }

            S Offline
            S Offline
            Super Lloyd
            wrote on last edited by
            #33

            I am consistent!!!

            If (one short liner or set value) put on the same line
            else if (one long line) put below
            else use brace!

            See, I even use if to define if behavior! Top that! ;P

            All in one Menu-Ribbon Bar DirectX for WinRT/C# since 2013! Taking over the world since 1371!

            1 Reply Last reply
            0
            • R RossMW

              Now, I am not interested in a VB versus C# debate but. In my job I only spend about 20% of my time coding. Being from a C# background anything new I would normally do in C# (and yes very occasionally VB), but I also have to maintain old VB code (and heaven forbid, very occasionally VB6). Normally I spent a month or two in each language (depending on the task at hand) and are happy in any camp. Changing back and forth between languages is relatively straight forward, but lately I noticed it takes me longer to get back into swing of C#. And the reason.. Well, I think its because I've got myself into bad c# typing habits from using vb. I seem to be wasting so time chasing missing / misplaced { }, forgetting semicolons case sensitivity and ()'s. Now semicolons, case and () problems are just a "Oh Bugger" moment, but as for missing / misplaced { } 's. They can be time waster. For all you pro c# developers, Do you have any tips on keeping these damn { } under control?

              S Offline
              S Offline
              Slacker007
              wrote on last edited by
              #34

              RossMW wrote:

              They can be time waster.

              They have never been a time waster for me. I have never given it a second thought. Because you don't do this much, you will notice "everything", I would guess.

              1 Reply Last reply
              0
              • R RossMW

                I main problem I seem to is after a lot of if, Switch or whatever and you end up with a lot of

                }
                }
                }
                }

                and then trying to figure which } belong with which {

                H Offline
                H Offline
                Herbie Mountjoy
                wrote on last edited by
                #35

                VS is good at matching braces and so is Notepad++. But if you wany to nest things so deeply you ought to put it in a separate class or function.

                I may not last forever but the mess I leave behind certainly will.

                1 Reply Last reply
                0
                • J JeremyBob

                  Not just that, but be consistent in your if statements if you don't want braces issue. i.e. ALWAYS use the braces, even in the above !condition case. Personally I find Resharper is an excellent tool to help "flatten" you code by suggesting inverting your if statements.

                  if (!condition)
                  {
                  return;
                  }

                  H Offline
                  H Offline
                  Herbie Mountjoy
                  wrote on last edited by
                  #36

                  True but I lke to see Return(); at the end of a function. Just makes it easier to read.

                  I may not last forever but the mess I leave behind certainly will.

                  1 Reply Last reply
                  0
                  • R RossMW

                    Now, I am not interested in a VB versus C# debate but. In my job I only spend about 20% of my time coding. Being from a C# background anything new I would normally do in C# (and yes very occasionally VB), but I also have to maintain old VB code (and heaven forbid, very occasionally VB6). Normally I spent a month or two in each language (depending on the task at hand) and are happy in any camp. Changing back and forth between languages is relatively straight forward, but lately I noticed it takes me longer to get back into swing of C#. And the reason.. Well, I think its because I've got myself into bad c# typing habits from using vb. I seem to be wasting so time chasing missing / misplaced { }, forgetting semicolons case sensitivity and ()'s. Now semicolons, case and () problems are just a "Oh Bugger" moment, but as for missing / misplaced { } 's. They can be time waster. For all you pro c# developers, Do you have any tips on keeping these damn { } under control?

                    E Offline
                    E Offline
                    Eric Whitmore
                    wrote on last edited by
                    #37

                    I am constantly formatting the document. In VS it is Ctrl+E, D. This keeps the braces matched up.

                    Eric

                    1 Reply Last reply
                    0
                    • R RossMW

                      Yes, very dimly. Makes it hide to find when you have to scroll to see it. Just have to remember the Left brace, right brace, back arrow, code... dance move...

                      M Offline
                      M Offline
                      Member 11495478
                      wrote on last edited by
                      #38

                      You can also use this to find the matching "{}" On the keypad use the Ctrl key on the right side of the keyboard. Press right Ctrl ] key combination. This will take you to the matching "{}".

                      1 Reply Last reply
                      0
                      • R RossMW

                        Now, I am not interested in a VB versus C# debate but. In my job I only spend about 20% of my time coding. Being from a C# background anything new I would normally do in C# (and yes very occasionally VB), but I also have to maintain old VB code (and heaven forbid, very occasionally VB6). Normally I spent a month or two in each language (depending on the task at hand) and are happy in any camp. Changing back and forth between languages is relatively straight forward, but lately I noticed it takes me longer to get back into swing of C#. And the reason.. Well, I think its because I've got myself into bad c# typing habits from using vb. I seem to be wasting so time chasing missing / misplaced { }, forgetting semicolons case sensitivity and ()'s. Now semicolons, case and () problems are just a "Oh Bugger" moment, but as for missing / misplaced { } 's. They can be time waster. For all you pro c# developers, Do you have any tips on keeping these damn { } under control?

                        B Offline
                        B Offline
                        BC3Tech
                        wrote on last edited by
                        #39

                        Use the Tools.[^]

                        1 Reply Last reply
                        0
                        • D Duncan Edwards Jones

                          Left brace, right brace, back arrow, code...

                          A Offline
                          A Offline
                          adriancs
                          wrote on last edited by
                          #40

                          { } Left + Top Enter ...Code...

                          1 Reply Last reply
                          0
                          • D Duncan Edwards Jones

                            Left brace, right brace, back arrow, code...

                            A Offline
                            A Offline
                            adriancs
                            wrote on last edited by
                            #41

                            { } Left Enter Top

                            1 Reply Last reply
                            0
                            • M Marc Clifton

                              RossMW wrote:

                              and then trying to figure which } belong with which {

                              Dang, doesn't the IDE (dimly, I'll grant) light up the matching braces? [on my high horse] If you have that much nesting, maybe you should break the function apart into smaller calls? [/on my high horse] Marc

                              Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project!

                              M Offline
                              M Offline
                              Member 10707677
                              wrote on last edited by
                              #42

                              Before they started colour matching the {}, I got into the habit fo placing a comment following the closing brace with an inverted reference to the starting brace procedure name.

                              The difficult may take time, the impossible a little longer.

                              1 Reply Last reply
                              0
                              • R RossMW

                                Now, I am not interested in a VB versus C# debate but. In my job I only spend about 20% of my time coding. Being from a C# background anything new I would normally do in C# (and yes very occasionally VB), but I also have to maintain old VB code (and heaven forbid, very occasionally VB6). Normally I spent a month or two in each language (depending on the task at hand) and are happy in any camp. Changing back and forth between languages is relatively straight forward, but lately I noticed it takes me longer to get back into swing of C#. And the reason.. Well, I think its because I've got myself into bad c# typing habits from using vb. I seem to be wasting so time chasing missing / misplaced { }, forgetting semicolons case sensitivity and ()'s. Now semicolons, case and () problems are just a "Oh Bugger" moment, but as for missing / misplaced { } 's. They can be time waster. For all you pro c# developers, Do you have any tips on keeping these damn { } under control?

                                T Offline
                                T Offline
                                The Great Kazoo
                                wrote on last edited by
                                #43

                                What I have been known to do: void SomeLongFunction { // many lines of code } // end void SomeLongFunction

                                1 Reply Last reply
                                0
                                • R RossMW

                                  Now, I am not interested in a VB versus C# debate but. In my job I only spend about 20% of my time coding. Being from a C# background anything new I would normally do in C# (and yes very occasionally VB), but I also have to maintain old VB code (and heaven forbid, very occasionally VB6). Normally I spent a month or two in each language (depending on the task at hand) and are happy in any camp. Changing back and forth between languages is relatively straight forward, but lately I noticed it takes me longer to get back into swing of C#. And the reason.. Well, I think its because I've got myself into bad c# typing habits from using vb. I seem to be wasting so time chasing missing / misplaced { }, forgetting semicolons case sensitivity and ()'s. Now semicolons, case and () problems are just a "Oh Bugger" moment, but as for missing / misplaced { } 's. They can be time waster. For all you pro c# developers, Do you have any tips on keeping these damn { } under control?

                                  A Offline
                                  A Offline
                                  Adam P L
                                  wrote on last edited by
                                  #44

                                  No just use Resharper it does it for you.

                                  1 Reply Last reply
                                  0
                                  • R RossMW

                                    Now, I am not interested in a VB versus C# debate but. In my job I only spend about 20% of my time coding. Being from a C# background anything new I would normally do in C# (and yes very occasionally VB), but I also have to maintain old VB code (and heaven forbid, very occasionally VB6). Normally I spent a month or two in each language (depending on the task at hand) and are happy in any camp. Changing back and forth between languages is relatively straight forward, but lately I noticed it takes me longer to get back into swing of C#. And the reason.. Well, I think its because I've got myself into bad c# typing habits from using vb. I seem to be wasting so time chasing missing / misplaced { }, forgetting semicolons case sensitivity and ()'s. Now semicolons, case and () problems are just a "Oh Bugger" moment, but as for missing / misplaced { } 's. They can be time waster. For all you pro c# developers, Do you have any tips on keeping these damn { } under control?

                                    M Offline
                                    M Offline
                                    Member11140258
                                    wrote on last edited by
                                    #45

                                    In VS you can just use ctrl+'}' on one of the braces to toggle to what VS thinks is the match. This can be done for any open and close types, html tag or ( ) etc. VS2013 will add both for you by default as you type.

                                    1 Reply Last reply
                                    0
                                    • D Duncan Edwards Jones

                                      That it pretty much is - definitely muscle memory at this stage.

                                      D Offline
                                      D Offline
                                      DerekT P
                                      wrote on last edited by
                                      #46

                                      Is it just me... VS2013 automatically creates the closing bracket whenever I type an opening...?

                                      J 1 Reply Last reply
                                      0
                                      • R RossMW

                                        Now, I am not interested in a VB versus C# debate but. In my job I only spend about 20% of my time coding. Being from a C# background anything new I would normally do in C# (and yes very occasionally VB), but I also have to maintain old VB code (and heaven forbid, very occasionally VB6). Normally I spent a month or two in each language (depending on the task at hand) and are happy in any camp. Changing back and forth between languages is relatively straight forward, but lately I noticed it takes me longer to get back into swing of C#. And the reason.. Well, I think its because I've got myself into bad c# typing habits from using vb. I seem to be wasting so time chasing missing / misplaced { }, forgetting semicolons case sensitivity and ()'s. Now semicolons, case and () problems are just a "Oh Bugger" moment, but as for missing / misplaced { } 's. They can be time waster. For all you pro c# developers, Do you have any tips on keeping these damn { } under control?

                                        A Offline
                                        A Offline
                                        Alexander DiMauro
                                        wrote on last edited by
                                        #47

                                        RossMW wrote:

                                        Do you have any tips on keeping these damn { } under control?

                                        Yes, it's called ReSharper[^] I've been using it since 2009 and I have no idea how to use Visual Studio anymore without it.

                                        I have always wished for my computer to be as easy to use as my telephone; my wish has come true because I can no longer figure out how to use my telephone - Bjarne Stroustrup The world is going to laugh at you anyway, might as well crack the 1st joke! My code has no bugs, it runs exactly as it was written.

                                        1 Reply Last reply
                                        0
                                        • S Sascha Lefevre

                                          You can change the color of the highlighting :) clicky[^]

                                          If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                                          D Offline
                                          D Offline
                                          DumpsterJuice
                                          wrote on last edited by
                                          #48

                                          What was the setting name you changed to do that? Where there's smoke, there's a Blue Screen of death.

                                          S 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