A Brief Look At NDependFri, Jun 11, 2021
I was recently (well, actually it’s more like 6 months ago now at the time of writing… things managed to get away from me!) contacted with an offer to give NDepend a go and see what it can do. As my current day job currently sees me writing C# and using the .NET ecosystem most of the time, I’m always interested in trying out new things.
NDepend is a static analysis tool for .NET. That is, the tool will examine a .NET project to help understand the code structure and provide suggestions on ways to improve your code quality. There are a variety of metrics, graphs and visualizations which can be applied to help give a directed overview of your project’s structure and any potential problems that may arise.
I’ve got some prior experience with static analysis tools for both .NET and for other platforms, such as ReSharper, so I was curious to see what NDepend had to offer. I was given access to a Professional license for NDepend and took it for a whirl on some of my projects. Here’s how I got on.
Initial Setup and First Impressions
Upon applying the license key I was generously provided to their website, NDepend downloaded a ZIP file containing several files. Included in the archive was a
README file; an executable for NDepend’s stand-alone Visual NDepend tool, which is cool - many tools I’ve used before such as ReSharper are just available as extensions for Visual Studio; a command line tool for running analysis; and an installer for the Visual Studio extension.
As having a stand-alone tool to do everything with was somewhat of a novelty, I spent most of my time fiddling with it. As expected, NDepend can also be integrated into your existing Continuous Integration pipelines to perform static analysis upon build, but I haven’t yet jumped into that side of things.
Booting up Visual NDepend presents a fairly familiar looking user interface reminiscent of Visual Studio.
From here, it was a case of following the very clear GEtting Started with NDepend guide. I loaded up a small project in Visual NDepend and after a couple of seconds of processing a HTML based report was opened in my web browser.
There is a lot of information presented here, but the most important thing to do is to investigate any Critical or Violated rules. In my case, there were a few:
Avoid having different types with same name
Don't call your method Dispose
Clicking on each of these yields some further details, such as the class where the rule was violated and some further information on what the rule means.
In addition to viewing the report as a HTML file, you can also view it within the Visual NDepend window itself, which presents the same information in a slightly different way and provides some further functionality such as a class and assembly browser.
One of the most powerful features of NDepend is their
CQLinq (Code Query Linq) query tool. This allows you to use the LINQ syntax familiar to most .NET developers to interrogate your code base.
For example, you can get a list of all methods by running a query such as the following:
from m in Methods select m
While this query isn’t immediately useful, once you start adding some conditions you can start to see the real benefit. For example, to get the details of all methods that start with the name
GetRead, you could run a query such as:
from m in Methods where m.NameLike("^GetRead") select m
Or you can even do something like get all the methods with more than 10 lines of code:
from m in Methods where m.NbLinesOfCode > 10 select m
You can run these queries within the Visual NDepend tool by navigating to the
Rules > View Editor Panel menu (or by pressing
CTRL + R on your keyboard).
You can use these queries to define your own custom rules, making the tool extremely flexible.
NDepend also makes it incredibly easy to view a graph of how your dependencies are linked and operate together. From the Visual NDepend user interface, selecting the menu
Graph > View Dependency Graph (or by pressing
ALT + G on your keyboard) will open the Dependency Graph view.
This graph can be zoomed and navigated using your mouse to pan the graph and zoom in on individual classes and methods.
The really nice aspect when it comes to a library is viewing which methods touch which variables. You can even select a variable to highlight all of the methods which reference it.
NDepend’s metrics view (
Metrics > Code Metrics View in the toolbar or
Ctrl + M on the keyboard) allows you to view and highlight various metrics within your code base at various levels.
For example, you can analyze the number of lines of comments on a method basis:
Or you can analyze the number of lines of code in each method
There are many other options and settings that you can use to customize which metrics are displayed and how they’re displayed.
I’ve only really started to scratch the surface of everything that NDepend can do. I haven’t even tried the Visual Studio extension yet, but the plethora of features that the Visual NDepend tool offers was already enough to keep me busy for a while.
If you’re looking for a static analysis tool or are just simply interested in seeing what NDepend can do, they offer a 14 day trial through their website.
Integration with Continuous Integration
NDepend provides pre-built integration into a variety of Continuous Integration systems such as Azure DevOps, Jenkins, and more. Unfortunately I haven’t yet had time to investigate these capabilities, but if the Jenkins integration with NDepend documentation is anything to go by it seems relatively simple. Perhaps I’ll get round to it in another six months time!