DotNetWise©

Saturday, May 23, 2009

Code Comment Tracking - A must have tool

23 June 2010: You might find this macro also intereting. It contains two useful methods:
Attach to W3WP so you never need to open the Attach To Process dialog.
Note that the macro is trying to attach to the first w3wp.exe process that is runs under your credentials. If you call the function again, it will attach to the next w3wp available, as sometimes you might have more than just one.
Of course if it doesn't find a w3wp.exe running under your account, it will attach to the first that it finds.

It also contains an Attach To IE function so you can easily debug javascript and attaching to IE.exe.
Note that calling the function multiple times, will attach to the next available IE.exe process.

For an easier use of these macros I suggest you should map your custom shortcuts - I am using alt+3 and alt+4 for these.
------------------------

How many times have are you looking through some old code and you can't figure out who created that 'weird' thing?
What about when is not your code , isn't hard to look after the fields, methods, properties etc. as they are not in the right order you are used with?



Here is a tool which can help you tracking and arranging the the code.

Have a look on this simple example:
It is weird aligned and it is not commented at all.
Usually
there is a certain order every developer respect. For example:
  1. Fields
  2. Constructors
  3. Properties
  4. Events
  5. Util methods
  6. Virtual/Override methods
  7. Business/Static methods
  8. Other classes, delegates, structs etc.
In the left image the code is messed up in a random order, hard to follow.
Of course this is just a small example, but on real code it gets very messy.


Well, that's about to be changed.

As you can notice the code is nicely arranged and well commented.
Now you know when a piece of code was actually created and who to blame on

Features:
  • Move code in regions
  • Group regions in an useful order
  • Author and date-time stamp for source-code tracking
  • Automatic xml-doc comment generated for properties i.e. Gets the Name - or - Gets or Sets the Date
  • Automatic xml-doc comment generated for event handlers i.e. Handles the Page's Load event
  • Automatic xml-doc comment generated for event raisers i.e Raises the Init event
  • Automatic xml-doc comment generated for constructors, classes, interfaces, delegates, structs, enums
  • The ability to add the comments/author/timestamp without moving all the code in regions
  • The ability to arrange the code (move in regions) without commenting it
  • The ability to move the code and comment it at the same time
  • The ability to comment the code with the currently logged in user (Environment.UserName)
  • The ability to comment the code with a customized/predefined username or timestamp

How to install it:
  1. Download the attached macro version for VS2008
    or for VS2010
  2. Open Visual Studio 2008/2010, and go to Tools->Macros->Macro Explorer (Alt+F8)
  3. Locate Samples -> Utilities, right click -> Edit
  4. Copy the code from the downloaded file and paste it right after Public Module Utilities
  5. Under project exporer right click References and Add Reference to System.Web.dll version 2.0

  6. Make sure you add on the imports section these lines:
    Imports System.Collections
    Imports System.Text
    Imports System.Text.RegularExpressions
  7. Save the samples (Ctrl+S) and go back in Visual Studio 2008/2010 on Tools->Options->Environment->Keyboard
  8. On 'Show command containing' type PutInRegionsWithoutCreatedAuthor
  9. On 'Press shortcut keys' type your favorite shortcut i.e. Ctrl+Alt+\ (Ctrl+Alt+Backshash) and press Assign
  10. On 'Show command containing' type CommentWholeCode
  11. Select CommentWholeCode if you like your windows login account name to be used for comments, or CommentWholeCodeSpecific if you want to override with your custom name
    To change the specific name locate the CommentWholeCodeSpecific method in the macros editor and fill in your desired name.
  12. On 'Press shortcut keys' type your favorite shortcut i.e. Ctrl+Alt+/ (Ctrl+Alt+Slash) and press Assign
  13. Now go to any class file (*.cs) and test the macros by pressing your shortcuts. eg. Ctrl+Alt+/ and Ctrl+Alt+\

Notes
  1. Sometimes VisualStudio goes crazy and shows up an error. To get around it just open the File Search dialog (Ctrl+F) and do a dummy search and run the macros again. The error should be gone. Monday, 6 July 2009, Laurentiu Macovei: This has been fixed and should work smoothly. Also tested on VS2010 and works.
  2. This version only works for .cs files located in CSharp (.cs) projects, or files from App_Code folder within a website
  3. Does not work for stand-alone files (out of solution files)
  4. Does not work for Visual Basic .vb files - just yet

The comment/put in regions operations can be undone by pressing Undo on your document so your document is safe.

The comment feature is intended to be used very often, by all the developers with no exceptions.
Basically every time when you add a new method, property, etc. you should run the CommentWholeCode macro (Ctrl+Alt+/) so it will be commented with your username. Otherwise, some other developer may 'take' your code under his username, and that's not desired.

If you have any questions, please let me know.

Happy code tracking!

5 comments:

  1. >Now you know when a piece of code was actually created and who to blame on

    Sorry, but doesn't every decent VCS out there support finding out who changed what and when, and in a much finer grained manner than this?

    ReplyDelete
  2. Ya, but you don't always have the code in the same VCS.
    I use this in parallel with the SVN comments and find it very useful over the years.

    Speaking so, it's very hard to see in 1000 revisions who did that particular method.

    ReplyDelete
  3. Works smoothly in VS 2008. Is there any plan to upgrade it to VS 2010 Beta 1 ? I'm installing it right now !

    Thanks.

    ReplyDelete
  4. Yup, just tested it on VS2010 and works smoothly as well ;)

    Just make sure you open the find dialog first and do some dummy search (the old VS bug still happens on vs2010) so no error "Value does not fall within the the expected range".
    Anyways, I have fixed this bug so just make sure you are using the latest version of the macro again.

    ReplyDelete
  5. WHEN WILL IT WORK WITH VB. Still its not working with vb

    ReplyDelete

 
Blog powered up by Blogger