Editorial Note: I originally wrote this post for the NDepend blog. You can check out the original here, at their site. Also, NDepend just released a new version that addresses tech debt extensively — check it out while you’re there!
If you’re familiar with NDepend, you’re probably familiar with the Visual Studio plugin, the out of the box metrics, the excellent visualization tools, and the iconic Zone of Uselessness/Zone of Pain chart. These feel familiar to NDepend users and have likely found their way into the normal application development process. NDepend has other features as well, however, some of which I do not necessarily hear discussed as frequently. The NDepend API has membership in that “lesser known NDepend features club.” Yes, that’s right — if you didn’t know this, NDepend has an API that you can use.
You may be familiar, as a user, with the NDepend power tools. These include some pretty powerful capabilities, such as duplicate code detection, so it stands to reason that you may have played with them or even that you might routinely use them. But what you may not realize is the power tools’ source code accompanies the installation of NDepend, and it furnishes a great series of examples on how to use the NDepend API.
NDepend’s API is contained in the DLLs that support the executable and plugin, so you needn’t do anything special to obtain it. The NDepend website also treats the API as a first class citizen, providing detailed, excellent documentation. With your NDepend installation, you can get up and running quickly with the API.
Probably the easiest way to introduce yourself is to open the source code for the power tools project and to add a power tool, or generally to modify that assembly. If you want to create your own assembly to use the power tools, you can do that as well, though it is a bit more involved. The purpose of this post is not to do a walk-through of setting up with the power tools, since that can be found here. I will mention two things, however, that are worth bearing in mind as you get started.
- If you want to use the API outside of the installed project directory, there is additional setup overhead. Because it leverages proprietary parts of NDepend under the covers, setup is more involved than just adding a DLL by reference.
- Because of point (1), if you want to create your own assembly outside of the NDepend project structure, be sure to follow the setup instructions exactly.
A Use Case
I’ve spoken so far in generalities about the API. If you haven’t already used it, you might be wondering what kinds of applications it has, besides simply being interesting to play with. Fair enough.
One interesting use case that I’ve experienced personally is getting information out of NDepend in a customized format. For example, let’s say I’m analyzing a client’s codebase and want to cite statistical information about types and methods in the code. Out of the box, what I do is open Visual Studio and then open NDepend’s query/rules editor. This gives me the ability to create ad-hoc CQLinq queries that will have the information I need.
But from there, I have to transcribe the results into a format that I want, such as a spreadsheet. That’s fine for small projects or sample sizes, but it becomes unwieldy if I want to plot statistics in large codebases. To address this, I have enlisted the NDepend API.