четверг, 4 декабря 2014 г.

My three real use cases of Type Dependency Diagram in ReSharper 9.0

Hi all!

As you may know, the new 9.0 version of ReSharper has the ability to show the dependencies between different types (classes, structs, interfaces, both from the source code or from assemblies) in a nicely layouted diagram. Frankly speaking, I'm not a big fan of diagrams, but it turned out that some cases can be really displayed in a diagram much more efficiently than via some old-school tools (tree presentation, or a plain list). For instance - 'Project Dependecny Diagram' in ReSharper 8 which can be really efficient in observing the architecture of my solution.

Lots of stuff can be done, by using new ReSharper type dependency diagram, but in this short article I want to share my 3 real use cases of how the Type Dependency Diagram can be used. Used for a real software engineering tasks, not for useless formal reports.

1. Observing

First of all this feature allows me to understand others code much more easily. Most often the specific subsystem's source code is located in a separate project or in a folder, or even in a bunch of projects located in a special solution folder. In all this cases I can build type dependency diagram easily from the Solution Explorer context menu.

If my initial scope of types is not big (~15 types) the diagram can really help with understanding what is going on here. It shows usages, aggregation and inheritance dependencies by default and I can also include so called 'Return type' and 'Ctor injection' usages.

If the scope of types I'm going to investigate is too big, there is no sense to display all these in on diagram - it would be just a useless mess. However, what might be useful is to group the big diagram by project structure, so the diagram will show me which folders depends on which.

The popup on the screenshot above is shown just after I hover the mouse on a folded edge. It can show me briefly what types are referencing each other, but of course I can expand folded node and see whole overview.
There are several ways to add more types to the diagram
1. Drag n drop files, folders, projects from Solution Explorer to the Diagram
2. Embedded Go-to-type that either navigates to already displayed types or adds new ones. Btw, I don't need even to place a caret there. Just start typing and that's it.
3. Rich Popup UI that allows me to add all related types to the specified node (both type's and project folder's nodes)
4. Drag'n'drop from text. For instance I can select the text with a type's name and drop it to the canvas.

2. Understanding the usage of external modules

Actually you might already knew about 'Show Modules External to Scope' ReSharper's feature and used it to investigate external usages of a module. The problem with this feature is its representation. You cannot really display many-to-many relations in a tree view. So on diagram I can explore such relations much more easily. To do that I can open a context menu on assembly reference(s) in Solution Explorer and click 'Show Usages On Diagram' button

For instance, I would like to see how do I use WPF libraries in my project. And ReSharper 9.0 solves this task pretty well. The following screenshot illustrates what I've got on the diagram:

Observing such diagrams is super easy for me and definitely much easier that exploring a tree view based UI.

3. Move to folder refactoring via Drag'n'Drop

ReSharper's 'Move to folder refactoring' is one of my favorites. It's priceless when I have to move lots of code between the projects in a big solution. In brief, ReSharper automatically analyzes and displays all possible conflicts for the desired move, then physically moves and updates all usages across the whole solution. So it turned out for me that this powerful refactoring is super efficient on a diagram!  Mostly because I don't need to look through a huge Solution Explorer's tree to find a specific project folder - all needed project folders are already on the diagram.
In the following example the class ReSharperDaemonVsBulbRemover is located in VB language folder - which is incorrect, it should be move up. Instead of searching the name of a parent folder, now I can perform a refactoring just with a single move.

ReSharper will notice me on any refactoring to avoid unnecessary drops :) I can also uncheck the toggle button to disable this functionality at all. In this case move a dragged node will just expand the container which might be useful for layouting purposes.

These are the most used cases for me personally. However I think that this tool might be useful for other real use cases. If you have some - please share!:)