Dela via


Map specific dependencies on code maps

When you want to understand specific dependencies in your code, visualize them by creating code maps (Visual Studio Ultimate only). You can then navigate these relationships using the map, which appears next to your code. This helps you track your place in the code while you work.

Code map - Map relationships in code

A green arrow shows where your cursor appears in the editor. You can make notes on a map, just like on paper.

Code maps help you avoid getting lost in large code bases, unfamiliar code, or legacy code. For example, when you’re debugging, you might have to look at code across many files and projects. Code maps help you navigate around these pieces of code and see relationships between them. You don't have to remember all this code, keep track of files, or draw a separate paper diagram. If you have to interrupt your work, code maps help refresh your memory about the code you're working on.

Create or open a code map

To create a code map you need Visual Studio Ultimate 2013 and a solution that has Visual C# .NET, Visual Basic .NET, X++ for Microsoft Dynamics AX, or C++ code (limited support). To get color-coded relationships, you'll need Visual Studio 2013 Update 3. To map X++ code and code that's shared across multiple apps you'll need Visual Studio 2013 Update 2 or later.

You can create a code map by:

  • Right-clicking an item such as a source file or a binary file in Solution Explorer and selecting Show on Code Map. This creates a code map for the selected item.

  • Dragging an item such as a source file or a binary file from Solution Explorer onto an open Code Map to include and/or highlight the item in the map.

  • Right-clicking an empty area of the editor window and selecting Show on Code Map. This creates a new empty code map.

  • Right-clicking an object or class in the editor window and selecting Show on Code Map. This creates a code map for the selected item. You can also find all references or show related items on a Code Map using this menu.

  • Clicking the Code Map button on the Debug menu when the application is paused.

  • From an open IntelliTrace file, or as a Dependency Graph from the Architecture menu.

You can view, make limited edits, and navigate code maps using Visual Studio Premium 2013 or Visual Studio Professional 2013.

Before you share maps with others who use Premium or Professional, make sure that all the items on the map are visible, such as hidden items, expanded groups, and cross-group links.

Watch code maps in action: Understanding complex code with code map (Channel 9)

Understand the problem

Suppose there's a bug in a drawing program that you're working on. To reproduce the bug, you open the solution in Visual Studio Ultimate and press F5 to start debugging.

When you draw a line and choose Undo my last stroke, nothing happens until you draw the next line.

Code map - Repro bug

So you start investigating by searching for the Undo method. You find it in the PaintCanvas class.

Code map - Find code

Start mapping the code

Now you start mapping the undo method and its relationships. From the code editor, you add the undo method and the fields that it references to a new code map. When you create a new map, it might take some time to index the code. This helps later operations run faster.

Code map - Show method and related fields

Tip

The green highlighting shows the last items that were added to the map. The green arrow shows your cursor’s position in the code. Arrows between items represent different relationships. You can get more info about items on the map by examining their tooltips.

Code map - Show tooltips

To see the code definition for each field, double-click the field on the map, or choose the field and press F12. The green arrow moves between items on the map. Your cursor in the code editor also moves automatically.

Code map - Examine field definition

Code map - Examine field definition

Tip

You can also move the green arrow on the map by moving your cursor in the code editor.

Understand relationships between pieces of code

Now you want to know which other code interacts with the history and paintObjects fields. You can add all the methods that reference these fields to the map. You can do this from the map or from the code.

Code map - Find all references

Change the layout to rearrange the flow of relationships and make the map easier to read. You can also move items around the map by dragging them.

Code map - Change layout

Tip

By default, Incremental Layout is turned on. This rearranges the map as little as possible when you add new items. To rearrange the entire map every time you add new items, turn off Incremental Layout.

Code map - Change layout

Let's examine these methods. On the map, double-click the PaintCanvas method, or choose this method and press F12. You learn that this method creates history and paintObjects as empty lists.

Code map - Examine method definition

Now repeat the same steps to examine the clear method definition. You learn that clear performs some tasks with paintObjects and history. It then calls the Repaint method.

Code map - Examine method definition

Now examine the addPaintObject method definition. It also performs some tasks with history and paintObjects. It also calls Repaint.

Code map - Examine method definition

Find the problem by examining the map

It seems that all the methods that modify history and paintObjects call Repaint. Yet the undo method doesn't call Repaint, even though undo modifies the same fields. So you think you can fix this problem by calling Repaint from undo.

Code map - Find missing method call

If you didn't have a map to show you this missing call, it might have been harder to find this problem, especially with more complex code.

Share your discovery and next steps

Before you or someone else fixes this bug, you can make notes on the map about the problem and how to fix it.

Code map - Comment and flag items for followup

For example, you can add comments to the map and flag items using colors.

Code map - Commented and flagged items

If you have Microsoft Outlook installed, you can email the map to others. You can also export the map as an image or another format.

Code map - Share, export, mail

Fix the problem and show what you did

To fix this bug, you add the call for Repaint to undo.

Code map - Add missing method call

To confirm your fix, you restart your debugging session and try to reproduce the bug. Now choosing Undo my last stroke works as you expect and confirms you made the correct fix.

Code map - Confirm code fix

You can update the map to show the fix you made.

Code map - Update map with missing method call

Your map now shows a link between undo and Repaint.

Code map - Updated map with method call

Note

When you update the map, you might see a message that says the code index used to create your map was updated. This means someone changed the code, which causes your map to not match the current code. This doesn't stop you from updating the map, but you might have to recreate the map to confirm that it matches the code.

Now you’re done with your investigation. You successfully found and fixed the problem by mapping the code. You also have a map that helps you navigate around the code, remember what you learned, and shows the steps you took to fix the problem.

See Also

Concepts

Map methods on the call stack while debugging in Visual Studio

Visualize code