Share via


How to: Explore Code with Dependency Graphs

In Visual Studio Ultimate, use dependency graphs to help you learn more about the organization and relationships in existing code. These graphs represent structures as nodes and relationships as links, which appear as arrows between nodes. To generate a graph, see How to: Generate Dependency Graphs for .NET Code.

The following example shows how the graph represents structures and relationships as nodes and links:

Dependency graph with nodes and links

Nodes and links on a graph

The following example shows how the graph represents structures and containment relationships as groups:

Dependency graph with grouped nodes

Grouped nodes on a graph

Warning

A graph shows only those dependencies for code that builds successfully. Components and their contents will not appear if build errors occur for those components. Therefore, make sure that a component actually builds and has dependencies on it before you make architectural decisions based on the graph.

In This Topic

  • Explore dependencies on the graph

  • Find areas of complexity

Explore Dependencies on the Graph

Dependencies on the graph are represented by the following kinds of links:

  • An individual link represents a single dependency between two nodes.

  • An aggregate link represents all the dependencies that travel in the same direction between two groups.

A cross-group link is just a link between two nodes across different groups.

Note

By default, the graph shows cross-group links only for nodes that are selected. To show these links at all times, on the graph toolbar, click the drop-down list next to the Change how links are rendered on the graph button, and then click Show All Cross-Group Links. If the graph toolbar is not visible, on the View menu, point to Toolbars, and then click Directed Graph.

Use the following tasks to explore the dependencies that interest you:

To

Follow these steps

See a list of dependencies that a link represents

  • Move the pointer over the link to see a tooltip that lists the dependencies.

Find a node by name

Use to search through multiple levels of grouped nodes.

  • Press CTRL+F.

Find the source or target of a link

Use this method when there are too many links to easily see the source or target of a specific link.

  1. On the graph toolbar, change the graph layout to tree layout or quick clusters layout.

    NoteNote
    If the graph toolbar is not visible, on the View menu, point to Toolbars, and then click Directed Graph.

    For more information, see How to: Browse and Navigate Graph Documents.

  2. Move the pointer over the link.

    Visual Studio displays two arrow buttons above the link. Visual Studio also shows a tooltip that identifies the source and target of the link.

  3. Click the arrow button that points in the direction that you want to follow.

    Depending on the direction that you selected, the source or target appears selected.

Examine the dependencies across nodes in different groups

  1. Expand the groups that interest you so that you can see all their children.

  2. Select all the nodes that interest you, including their children.

    To select all of the nodes in a group, press and hold the left mouse button as you draw a rectangle around that group. To select all of the nodes on a graph, press CTRL+A.

    The graph shows the cross-group links between the selected nodes.

    NoteNote
    To show cross-group links at all times, on the graph toolbar, click the drop-down list next to the Change how links are rendered on the graph button, and then click Show All Cross-Group Links. If the graph toolbar is not visible, on the View menu, point to Toolbars, and then click Directed Graph.

Examine the dependencies behind an aggregate link

NoteNote
This works only when each end of the link is connected to an assembly. If there is no assembly information, then a graph cannot be created.
  1. On the graph toolbar, change the graph layout to tree layout or quick clusters layout.

    NoteNote
    If the graph toolbar is not visible, on the View menu, point to Toolbars, and then click Directed Graph. For more information, see How to: Browse and Navigate Graph Documents.
  2. Double-click the aggregate link that interests you.

    - or -

    1. Move the pointer over the link.

      Visual Studio displays two arrow buttons above the link.

    2. Click the plus sign (+) between the two arrow buttons.

  3. In the Generate Dependency Graph dialog box, click the levels of nodes that you want to include on the graph.

    For example, to examine dependencies from the assembly level all the way through to the method level, click Assemblies, Namespaces, Types, and Methods. If you want to see only the dependencies between assemblies and classes, click only Assemblies and Classes.

    To include the contents of a method on the graph, use Architecture Explorer to find those elements and then add them to the graph. For more information, see How to: Find Code Using Architecture Explorer.

  4. Make sure that Show Containment as Group is selected, and then click OK.

    Visual Studio generates and displays a graph with the levels of nodes that you selected and their dependencies.

  5. On the graph, select the groups that interest you, right-click a selected group, point to Group, and then click Expand.

    - or -

    Right-click the graph, point to Group, and then click Expand All.

    The graph expands the groups to show their children.

  6. Select all the nodes that interest you, including their children.

    To select all of the nodes in a group, press and hold the left mouse button as you draw a rectangle around that group. To select all of the nodes on a graph, press CTRL+A.

    The graph shows the cross-group links between the selected nodes.

Find Areas of Complexity

Use a dependency graph to identify areas of code that you might be able to improve. A well-formed dependency graph will show a minimal number of dependencies. For example, you can:

  • Find code that has loops or circular dependencies.

    Examine these areas to see whether you can simplify them. Consider whether you can break these cycles.

  • Find code that has too many dependencies.

    Examine these areas to see whether they are performing too many functions or to determine the impact of changing these areas. To make code easier to maintain, change, test, and reuse, consider whether you can refactor these areas so that they are more clearly defined, or whether you can merge code that performs similar functions.

  • Find code that has no dependencies on it.

    Examine these areas to see whether they are necessary or whether you should remove this code.

You can run specific analyzers on these graphs to help you perform these tasks. These analyzers continue to run after you apply them. If you change the graph, any applied analyzers will automatically reprocess the updated graph. To stop running an analyzer, right-click the analyzer in the Legend box, and then click Delete.

Note

    If you have a very large graph, running an analyzer might cause an out of memory exception. If this occurs, edit the graph to reduce its scope or generate a smaller one, and then run the analyzer.

To find areas of complexity in code

  1. Make sure the graph layout is set to tree layout or quick clusters layout.

    For more information, see How to: Browse and Navigate Graph Documents.

  2. Right-click the graph, point to Analyzers, and then click one of the following:

    Analyzer

    To identify nodes that

    Circular Reference

    Have circular dependencies on each other

    NoteNote
    Circular dependencies inside the Generics group are not shown on the graph when you expand the group.

    Find Hubs

    Are in the top 25% of highly-connected nodes

    To select these nodes and hide all others on the graph

    1. Right-click the graph, and then click Show Advanced Selection.

    2. In the Selection box, expand Properties, and then Hub.

    3. Under Hub, click True.

      The top 25% of the most highly-connected nodes are selected.

    4. Right-click the graph, point to Selection, and then click Hide Unselected.

      The graph hides the unselected nodes, and the analyzer identifies new nodes as hubs.

    You can also identify dense areas of dependencies by switching the graph layout to dependency matrix view. For more information about dependency matrix view, see How to: Browse and Navigate Graph Documents.

    Unreferenced Nodes

    Do not have references from any other nodes.

    Caution noteCaution
    Verify each of these cases before assuming that the code is not used. Certain dependencies such as XAML dependencies and run-time dependencies cannot be found statically in the code.

See Also

Concepts

How to: Generate Dependency Graphs for .NET Code

How to: Browse and Navigate Graph Documents

How to: Edit and Customize Graph Documents