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?
I code in VB and C# at work - my preference is for C~ due to what I describe as its 'elegance'. Occasionally when I swap over I catch myself adding a semicolon to some VB code however Visual Studio catches it and I soon remember that I am coding on VB. I don't find it too big a deal - being bilingual(English and French), for me it's a similar experience swapping between two programming languages and two spoken languages. Sometimes when I see some horrendous VB code I am heard to exclaim "Put*in de b*rdel de m*rde!" when I meant to comment in English
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
-
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?
As others have said, I close braces on opening them (well, I cheat and let ReSharper do that), and I keep the number of braced scopes in a method to a minimum, e.g. avoid nested braces like the plague. The highest level I normally get to is say for one loop or switch at a time.
No object is so beautiful that, under certain conditions, it will not look ugly. - Oscar Wilde
-
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?
I stick to languages derived from c. Currently mostly c, java and perl. I have no problems switch between these. Pitch to one of your managers to have the code you're maintaining rewritten in C#... Or rewrite it in C# and tell your boss what you've did and if they'd be interested in changing over to it. To my knowledge there should be no reason to write any code in vb... If we employ people, it is never a requirement to only know vb or only know C#... Its usually in the lines of, do you have vb or C# experience. As the task at hand can usually be performed in either. According to Wikipedia there are 89 different c derivative languages. C obviously had a winning recipe. Your subject should read 'Damn vb (why do they have to think they are special and different)'.
"Program testing can be used to show the presence of bugs, but never to show their absence." << please vote!! >>
-
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?
* 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) -
The keyboard shortcut "control }" will jump to the matching bracket if you can't immediately identify it. It will work when the cursor is either before or after an opening bracket or a closing bracket. I'd use that command every 5 minutes. very helpful when the scope of a set of brackets extends beyond the visible portion of the page.
who knows what evil lurks in the hearts of men?
-
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?
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.
-
* 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) -
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.
-
You're welcome :)
-
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!
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;
} -
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;
}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 defineif
behavior! Top that! ;PAll in one Menu-Ribbon Bar DirectX for WinRT/C# since 2013! Taking over the world since 1371!
-
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:
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.
-
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 {
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.
-
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;
}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.
-
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?
I am constantly formatting the document. In VS it is Ctrl+E, D. This keeps the braces matched up.
Eric
-
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...
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 "{}".
-
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?
-
Left brace, right brace, back arrow, code...
-
Left brace, right brace, back arrow, code...