views:

79

answers:

2

Greetings Friends,

What is the best way (least amount of keystrokes) to get Visual Studio 2010 to automatically insert the current date and my name/initials whenever I put a single line comment into my codebase? This should support C# and it'd be even better if it worked in my .aspx pages too.

Thanks -- I know someone out there has the perfect solution :).

+3  A: 

Create a macro and assign a Shortcut key.

The easiest way is go to Tools->Macros->Macro Explorer and edit one of the samples, I used Samples->VSEditor, right click that one and edit. Now youre in the Macro editor Now create this function.

Sub NewCommentLinePersonal()
    Dim textSelection As EnvDTE.TextSelection

    textSelection = DTE.ActiveWindow.Selection
    textSelection.NewLine()
    textSelection.Insert(Utilities.LineOrientedCommentStart())
    textSelection.Insert(" " + Date.Now + " - Your Initial ")
End Sub

then go to Tools->Options->Environment->Keyboard and type the NewCommentLinePersonal on textbox "Show commands containing:" then choose your shortcut key

Raymund
Nice work! Thank you.
Buffalo
+3  A: 

Perhaps another way of approaching it, assuming the insertion of a timestamp and name is being done for change tracking, is to lean on source control.

For example, in my current codebase, we deprecated the use of putting in change comments, since we found that the field of green was cluttering things, and if I ever needed to see who changed what, I could simply look in our source control system, and even see how this one change was related to other modifications within the same changeset.

Bob Palmer
That is a very good suggestion. The comments I've been making in the code are mostly to remind myself why I decided to re-engineer certain areas. I inherited spaghetti code, so now I've got maintenance to do plus the extra hacking to "align the noodles" ... haha!
Buffalo
Been there done that ;) On a side note, I strongly recommend picking up the book 'Working Effectively with Legacy Code' by Michael feathers. If you are working with an inherited codebase, the entire book is pure gold.
Bob Palmer
I completely agree. Do not spam your initials and the date all over the source code. It looks awful when there is too much of it.
C Johnson