4

When I'm writing comments in my code, I often forget to add the initial space after the comment identifier.

'this is a comment

when really it is supposed to be

' this is a comment

I realize this is quite trivial, and you could simply say "just add the damn space you idiot", but I'd really like to automate this so that I just don't have to worry about it.

Can anyone point me in the right direction of an elegant way to add the comment space?

note I do realize that a catch all string replace or regex replace could screw up other things ... IE:

Dim something As String = "I'm a nerd"

would actually come out

Dim something As String = "I' m a nerd"

So the way I see this being resolved is if it's only on a line by it's self and is not followed by a second single quote... IE: '' would not trigger the replacement.

2
  • I think you are being obsessive/compulsive. :O) I actually like no space.
    – AMissico
    Jul 12, 2010 at 11:02
  • Note that the space/lack of is normally used to differentiate a comment and commented code. C# code normally uses // and ////, with // being ambigious.
    – Deanna
    May 2, 2012 at 12:12

3 Answers 3

2

You could always get a copy of resharper and one of the rules in there is what you are describing. Once you finish with your code you can do a format on the whole file or even solution and it will globally fix that rule for you.

2
  • I actually have resharper... I'm more so wondering about doing this on the fly (real time) Jul 5, 2010 at 20:38
  • 1
    I can't find this feature in the VB.NET version of ReSharper Jul 10, 2010 at 23:30
1

This would be a pretty good case for an editor extension. You can detect when a line is whitespace, apostrophe, not white space and either insert the space or put a decoration so you will learn to follow the pattern. If you've moved to 2010, consider it - they are really pretty easy to write.

2
0

I had and have similiar problems. This is a habit which you can change. Yet, sometimes your brain is just wired to make the same mistakes. For instance, no matter what I do, I always type data instead of date.

You can change your behavior. Find some method that you find helpful for changing habits. Create a "personal code review" checklist and add this item. After a few months, you will find the space comes naturally.

If this is one of the "hard-wired" brain goofs, then create a *Visual Studio" macro that visit's CodeDom. Through the CodeDom namespace it is easy to find comments and make changes as needed. Why use regular-expressions?

If you want to use regular-expressions, because you are familiar and it is easier, then create a better expression to avoid potential errors.

Refactoring VB.NET code with regular expressions at http://www.vbmigration.com/Blog/post/2008/07/Refactoring-VB%2cNET-code-with-regular-expressions.aspx should be helpful in creating better regular-expressions.

1
  • I am working on breaking the habit, but it's a hard one to break. Jul 12, 2010 at 22:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.