Visualize code with Visual Studio 2012’s Code Map

he Code Map feature of Visual Studio 2012 lets you visualize your code to more easily navigate its complexities. This allows you to point and click your way through complex code hierarchy as opposed to searching through countless lines of code.

Getting Code Map

The Code Map feature is included in the Visual Studio 2012 Update 1 download. It is available in three Visual Studio 2012 versions: Ultimate with MSDN, Premium with MSDN, and Professional with MSDN; however, only Ultimate includes the full version with no restrictions. Premium requires Team Foundation Server instance and client license. Professional includes a read-only version. I use the Ultimate version in this post.

Seeing is believing

Once your instance of Visual Studio 2012 is updated, the Code Map feature will be available in a context menu when inside your code. A simple click of the right mouse button, and the menu shown in Figure A appears. It includes these options:

  • Show On Code Map is the core feature that generates the Code Map for the current solution.
  • Show Related Items On Code Map allows you to expand the default Code Map to include other items related to the code; this includes displaying base types, derived types, references, and the containing namespace. Figure B shows this menu expanded.

Figure A

The Code Map feature accessed by right-clicking within your code. (Click the image to enlarge.)

Figure B

A simple C# console application presented by a Code Map. (Click the image to enlarge.)

Figure B shows the Code Map feature in action as we take advantage of some of the options available in Figure A. I selected Show On Code Map, followed by showing the containing namespace and showing base types. Everything is presented in a visually pleasing format. When you hover over individual items in the diagram, more information about it displays (like signature for methods).

The map in Figure B was generated using a basic C# console application that includes two classes: Test and Program. The Test class includes four methods. Figure A is shown with the Test class expanded (this is achieved by positioning your mouse over a class in the diagram and clicking the down arrow). In addition, it includes comments (in yellow), and the classes are contained in their namespace with the whole thing wrapped in its executable filename. The bottom box in the diagram includes the externals for the application. In this case, the code utilizes the base System.Object class.

An additional context menu is available when the mouse is placed over the generated Code Map. Figure C shows these options; this includes Show Containing Assembly, which was used in Figure B where it showed the containing exe filename. Also, it provides editing options where you can insert comments, flag items for follow-up, and choose different color options. The comment option was used in Figure B to add the two comments that appear in yellow with arrows pointing to noted items.
Figure C

Additional formatting available via a context menu within Code Map. (Click the image to enlarge.)

The Code Map feature is fully interactive. It allows you to rearrange items within the diagram as well as other options available in the menu at the top of the Code Map window visible in Figures A and B. The options include Undo, Show Related Items, Layout, Share, Zoom, and Legend. The Share option lets you save or email the diagram as an image or XPS.

Directed Graph Markup Language

Beginning with version 2010, the Visual Studio suite of products utilizes an XML language called Directed Graph Markup Language (DGML) to describe and define graphs. DGML is used to generate Code Maps in Visual Studio 2012. You can view the DGML source via the View DGML option in Figure C. The result of this selection is shown in Figure D. This gives you the option of directly manipulating a Code Map via DGML, which seems contrary to the point of the visualization, but it is an option.
Figure D

Working with a Code Map via its DGML source. (Click the image to enlarge.)

Working with the code

The Code Map is a nice tool for visually working with your code. It allows you to easily navigate, annotate, and alter code without searching through many lines of code. While it provides a pretty visualization, I cannot see myself utilizing it in future projects. I tried it with more complex code than what was presented in this post, but I prefer to work directly with the code and various other Visual Studio features (like the Solution Explorer and Object Browser to navigate its complexities). Maybe this is a sign of my age, but I will stick with my old methods. Please give this new feature a test drive to determine if it helps with your daily tasks.

Keep your engineering skills up to date by signing up for TechRepublic’s free Software Engineer newsletter, delivered each Tuesday.