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 1 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.
  • 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
              • D DumpsterJuice

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

                S Offline
                S Offline
                Sascha Lefevre
                wrote on last edited by
                #49

                Tools > Options > Environment > Fonts and Colors > Brace Matching (Rectangle) Just overlooked it myself first on first try ;)

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

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

                  P Offline
                  P Offline
                  patbob
                  wrote on last edited by
                  #50

                  Find matching brace (most code editors have one) -- Visual Studio: ctrl-], Vi: % Use a smart editor that automatically adds, shows or otherwise assists in making sure you have balanced brackets. Consistent indentation. It also helps not to have large blocks of code, but sometimes that just can't be avoided. It is a pain sometimes, but I've just learned to live with it since the previous alternatives were much, much worse.

                  We can program with only 1's, but if all you've got are zeros, you've got nothing.

                  1 Reply Last reply
                  0
                  • S Sascha Lefevre

                    Tools > Options > Environment > Fonts and Colors > Brace Matching (Rectangle) Just overlooked it myself first on first try ;)

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

                    Thanks for such a quick reply! > from Charlotte, NC, USA Where there's smoke, there's a Blue Screen of death.

                    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?

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

                      How long is on average one of your methods? A method shouldn't be longer than a screen, which makes around 50 lines of code. This will solve your braces problem immediately.

                      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?

                        F Offline
                        F Offline
                        fglenn
                        wrote on last edited by
                        #53

                        I don't know about anyone else, but when I enter a '{', I also enter the corresponding '}' before I insert any code in between the braces. I never have a problem with unbalanced/misplaced braces.

                        Fletcher Glenn

                        1 Reply Last reply
                        0
                        • D DerekT P

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

                          J Offline
                          J Offline
                          JRickey
                          wrote on last edited by
                          #54

                          It does for me, also. I still have the default setup. Note that it also does that in text documents, when I don't want it to. And in code, typing the closing element can add an extra one (I think that happens if I use cursor keys to edit before closing).

                          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!

                            T Offline
                            T Offline
                            TNCaver
                            wrote on last edited by
                            #55

                            Marc Clifton wrote:

                            Dang, doesn't the IDE (dimly, I'll grant) light up the matching braces?

                            Only when you put the cursor just after the } or just before the {. Not if the cursor is inside the block in question.

                            If you think 'goto' is evil, try writing an Assembly program without JMP.

                            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?

                              J Offline
                              J Offline
                              James Curran
                              wrote on last edited by
                              #56

                              Obviously, you are using the god-forsaken "K&R" bracing style

                              if (condition) {
                              /// stuff
                              }

                              When you choose a coding style based on readability rather than historic fanboy popularity, these problems go away:

                              if (condition)
                              {
                              // Stuff
                              }

                              Truth, James

                              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?

                                U Offline
                                U Offline
                                User 4598947
                                wrote on last edited by
                                #57

                                When there's too much nesting, and therefore too many braces, consider 1) refactor out inner nestings into new methods 2) label }'s:

                                namespace foo
                                {
                                public class bar
                                {
                                public void fu()
                                {
                                for(var int i = 0; i < 10; i++)
                                {
                                if(Math.PI != 0.0)
                                {

                                            } // if PI not 0
                                
                                        } // for i
                                
                                    } // fu
                                
                                } // bar
                                

                                } // foo

                                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
                                  Mark Miller
                                  wrote on last edited by
                                  #58

                                  Productivity Power Tools [^] includes Guidelines which are helpful. VSCommands [^] has a "Code Block End Tagger" that will show a "tag" on the closing brace either all the time or only when the opening brace is not in view.

                                  Sincerely, -Mark mamiller@rhsnet.org

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

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

                                    FWIW In VS, set focus to a brace, CTRL_} finds the matching brace.

                                    There are strangers on the Plain, Croaker

                                    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
                                      brianriggan
                                      wrote on last edited by
                                      #60

                                      Indent Guides https://visualstudiogallery.msdn.microsoft.com/e792686d-542b-474a-8c55-630980e72c30[^] It is a visual studio addin that will help you figure out which blocks belong together. The defaults look horrible but you can custimize them. I have each level of indentation a different color. This is one addin that I can't live without.

                                      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?

                                        N Offline
                                        N Offline
                                        nocturns2
                                        wrote on last edited by
                                        #61

                                        In Visual Studio, you can press Ctl-K & Ctl-D that will format your document. Same as pressing Edit>Advanced>Format Document. That will give you an indication of where the faulty area is, then, you can follow the {}'s via indention. Moving from VB to C#, for a while, I followed the convention of put in all the decorations first, before inserting code. What got me was how after a period of time using c#, coding vb, I felt like I was leaving stuff out, ... in particular the semi-colons.

                                        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 {

                                          K Offline
                                          K Offline
                                          KP Lee
                                          wrote on last edited by
                                          #62

                                          You could always:

                                          }//End of switch logic
                                          }//End of if peter knows jane
                                          }//End of while loop
                                          }//Finally! end of dam program TG

                                          Of course then you would have the never put comments in code police on your tail. Also, you'd have to pray your comments are relative to what has really ended. In case you are wondering, TG is the same as in TGIF.

                                          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