I’d like to take a closer look at the use of CodeMaps in Visual Studio to debug and to document our solution. The following link to Architecture and Modeling has some very cool videos detailing how to use Code Maps to find the root cause of a bug. Further to debugging Code Maps can be used to assist in documenting program flow and dependency chains visually. Lets start with a very basic example of how to generate a CodeMap. First off if you don’t know what a Code Map is then let me explain. When browsing your VS project files and looking at the ‘References’ link that appears above a method declaration, if you pay careful attention you’ll see the link ‘Show on Code Map’. See below.
Once you click this link a new Code Map file is created containing the referenced method and calling context. This is a good starting point to build the remaining Code Map. Your Code Map will look something like the illustrated version below.
From here you can change the Layout of the Code Map by clicking on the ‘Layout’ menu option at the top and selecting from the host of available options. I’ve selected ‘Top to Bottom’ and the layout now look like this.
As you can see the calling context is above the method. If I now want to browse to the definition of the method I can simply right click the method of choice and select ‘Go To Definition’. Further if I want to see what additional methods are being called and have them added to the Code Map I can simply right click on the update method and select ‘Show Methods This Calls’. So now we have a pretty picture, what about adding some comments? Well that can quite simply be done by right clicking and selecting ‘New Comment’. Type something of meaning and arrange the comment as you wish. The Code Map diagram is now showing the top down view of the calling context, the method in question and we’ve added a comment about the method. This is great for illustrating program flow and documenting how a system works to a newbie or your peers.
Great but now what!?
In the next post, we will look closer at how to use Code Maps to debug a problem that isn’t obvious when stepping the code but becomes far clearer when using a Code Map.