Damn c# { }'s
-
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?
-
Is it just me... VS2013 automatically creates the closing bracket whenever I type an opening...?
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).
-
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!
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.
-
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?
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
-
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?
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
-
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?
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
-
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 {
-
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?
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.
-
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?
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.
-
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 {
You could always:
}//End of switch logic
}//End of if peter knows jane
}//End of while loop
}//Finally! end of dam program TGOf 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.
-
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?
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 :)
-
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?
-
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?
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?
-
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 {
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?")
-
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?
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.
-
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
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.