I’m always on the lookout for ways to improve my code. Using automated tools is one of the easiest. I still program in C++ as well as C#. Eons ago in C++ a tool named “lint” became the defacto standard for doing static code analysis. It is very thorough and can find bugs you didn’t know you had. A lot of programmers don’t use it though because it is hard to setup and use and on an existing code base it can generate tons of errors. In the C# world there are some much better options. I for one am a big fan of ReSharper. It can find not only errors but can suggest ways for you to leverage tools like LINQ to streamline your code. It’s good, fast easy to use and integrates nicel into Visual Studio. It also has the advantage that code analysis isn’t even its reason for being, it was originally designed as a productivity enhancement tool for the editor and a refactoring engine.
NDepend on the other hand appears to be designed from day one as a code analysis/exploration tool on steroids. It too can be integrated into Visual Studio (and Reflector) and it has the added advantage of being run as a stand-alone tool in case you want to keep your Visual Studio environment in a virginal state (particularly nice when you're just evaluating a tool).
Getting up and running
I would like to disclose that the fine folks that make NDepend offered me a free license if I would consider writing a review in my blog. I wasn’t required to write the blog and I certainly wasn’t required to write a favorable review. Now the generous offer probably produces a favorable bias, but I think this product truly deserves any and all the accolades I'm giving it. Plus they obviously think highly of you, my seven readers, and consider you all forward-thinking professionals that would be highly interested in such a tool.
After downloading the application and installing the license file, it ran with no problems. The Opening Screen is clean and easy to figure out. The screenshot shows how it looked after I created my first two projects to analyze. There were a couple of links to videos but I decide to take it for a spin without even that advantage.
I used the file menu to create a new project and was shown the screen at left. The green arrows shows the button I clicked next. This opened a standard file open browser dialog so I could browse to my Visual Studio solution file.
The screen updated to show my my selected assembly on the left and the assemblies it was using on the right. I then used the Analysis Menu to run my first analysis. I immediately saw the analysis running in the message pane and it finished in about 15 seconds (it was a small project). At the conclusion I had two new windows as shown.
I then clicked on the Show NDepend Interactive UI Graph and several new panes were opened. Most things on the screen update the info pane on the right when you move over them. I was interested in any problems the analysis found so my eye was caught by both the yellow Warnings tab in the Error List and the yellow warning icons in the CQL Query Explorer. I opened the Code Quality icon, clicked on the Methods too big rule and the CQL Query edit pane bottom portion showed me the offending method, as shown in this image:
As I explored the various queries, I was interested in learning more about some of the queries. Fortunately, most of them contain a hyperlink to just that information in top pane of the CQL Query Edit area on the left.
I made my window reasonably small so that my screenshots weren't gigantic, but you'll want to have a large monitor and a large window size when using this tool.
The warnings at the bottom were related to the fact that I had recently moved the project folder on my hard disk so NDepend was letting me know that paths in the debugger files were not accurate. A quick rebuild solved that.
I switched to the Class Browser tab and drilled into my class. I noticed some files were missing (in particular the derived classes). The info pane showed that for all my files, that the analysis was incomplete because some source code was missing. This remained true after I rebuilt the project so I'm going to have to watch the videos or crack open the manual to find out why. Right clicking on classes, methods and fields gets you a plethora of options. I liked that I could explore from here the callers/callees of any method – Similar to F12/Shift F12 inside Visual Studio.
NDepend is ridiculously easy to set up and use and get value from immediately. Exploiting its full potential is going to take some more time and I'm sure will be worthy of another post. A lot of what I looked at in my initial foray is information I'm already familiar with from tools like Lint and to some extent ReSharper. However, as I explored many of the other features (that I didn't review here) I can already see some benefits that NDepend provides that the other tools don't. The Metrics graph (shown at left for one gives you a very interesting view of the relative sizes of your modules. Hovering over any block will show you the details on that block including the method name and number of lines of code. This would quickly let you target those big balls of mud in the project you inherited. The dependency matrix and graph allow you to get a bird's eye view on the total project as well as dig down into a detailed view to see dependencies on a method. The screen shot below was taken after I drilled down into a single method on my TouchScreen class.
Clicking on the System Drawing row shows me exactly what is going on as shown below:
Whether you work alone or on a large team, NDepend is worth checking out. If you're new to static analysis checking this is a great way to start. If you're an old hand at setting up lint, this tool is enough to make you never want to program in C++ again. There is a free trial version available so you have no excuse for not taking it for a spin. It removes a few features (like the HTML report) but it is generously time limited. As I write this post, the current trial won't expire for over 4 months. Now excuse me, I have some videos to watch, documentation to read and an iceberg to uncover.