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.
  • 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
                              • 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
                                Umair Zuberi
                                wrote on last edited by
                                #63

                                Hey, Go to Tools->Extension Manager. There are many useful extensions there for you like "Brace Completer" which will automatically puts '}' when you type '{', "Code alignment" for aligning your code automatically, "highlight all occurrences of selected word", "word wrap with auto-indent", "JScript Editor Extensions" for many useful JavaScript extensions. Hopefully it will help you better coding :)

                                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
                                  pt1401
                                  wrote on last edited by
                                  #64

                                  I often comment the closing brace, saves a lot of brace-matching but does add some clutter:-

                                  ...
                                  } // if
                                  } // MyMethod
                                  } // class
                                  } // namespace

                                  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
                                    Frank Peelo
                                    wrote on last edited by
                                    #65

                                    RossMW wrote:

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

                                    Always, always, always have my opening and closing brackets in the same row or column. And put comments after }, as Member 10707677said. Trying to figure out where the missing { is when people put the opening { on the same line as the function or if, is just making life unnecessarily hard. And sometimes I come across conditions that are so ugly, I have to do it there too...

                                    if (((x==0) && (y==1)) && (z==2) && ((w==3) && foo==bar))

                                    if ( ((x==0) && (y==1))
                                    && (z==2)
                                    && ((w==3) && foo==bar)
                                    )

                                    Occasionally, I have to go and ask whoever wrote it... why?

                                    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 {

                                      U Offline
                                      U Offline
                                      User 11383510
                                      wrote on last edited by
                                      #66

                                      I love the code completion features in my editors. I use Visual Studio and Sharp Develop. When I type in if and hit tab it stubs in the braces and brackets. For and if/else I use ife tab. After the closing bracket I put in two slashes and a comment following the block. This way when I run into "dribble" (the stream of closing brackets yo showed) each on of the closing brackets hass a comment identifying what that bracket closes. It's tedious to get into the habit but it helps late at night when you are debugging things line by line and you get distracted by the family in your home office. ("Where the %^#$& did I leave off?")

                                      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 10523452
                                        wrote on last edited by
                                        #67

                                        If you are working in Visual Studios 2013, there's a NuGet package I would recommend for this very issue. Particularly, to add to the brace matching-coloring, this package adds a tag to your closing braces. This tag tells you what the brace is ending (method name, switch statement, for-loop, etc), and is clickable to return to the opening brace. See here for info on the VSCommands for Visual Studio 2013 package.

                                        1 Reply Last reply
                                        0
                                        • M Mark Miller

                                          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 Offline
                                          U Offline
                                          User 10523452
                                          wrote on last edited by
                                          #68

                                          This. I consider both packages a must. Also, VSCommands' tag has some important advantages. In addition to telling you what the curly brace is ending, it is clickable - navigating you back to the matching opening curly brace.

                                          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