NDepend: First Impressions From a Static Analysis Newbie
A tool that has recently come to my intention is NDepend, a static analysis tool for .NET. This article is a summary of my first impressions with NDepend.
Join the DZone community and get the full member experience.Join For Free
a tool that has recently come to my attention is ndepend , a static analysis tool for .net. this article is a summary of my first impressions with ndepend.
disclaimer: my licence for this product was supplied by ndepend, but this is an unbiased review based on my usage so far.
what is static analysis?
static analysis is the analysis of your code. it is performed without running the code, hence static, and will report on things such as:
- code style.
- project dependencies.
- code contracts.
- best practices
getting started with ndepend
there is some great documentation out there to get you up and running with ndepend. the getting started section of their website is a great place to start. there is also a pluralsight course , which i found to be useful.
once ndepend is installed, everything can be accessed through the ndepend toolbar option in visual studio.
ndepend gives you insights into the health of your code base as a whole. often, we look at our code at a class level. ndepend lets you see the bigger picture. from that, you can make informed decisions on a variety of things ranging from architecture to code quality to unit test coverage. there is a lot you can analyze with ndepend, and it can be quite daunting to begin with, so i am going to cover my top features to look at as an ndepend newbie.
the dependency graph is a fantastic way to explore your code base and to get an understanding and overview of the project architecture. by default, the size of the box for the assembly is proportional to the lines of code. i like this feature, as i can immediately understand where the meat of the project is. if this was a project that i was looking at for the first time, i would know which assemblies contain the majority of the code.
it also displays the dependencies of the assemblies on each other. i ran this tool on an old project with an n-tier architecture. straight away, the graph was able to show me that the web layer had a dependency on the data layer. this was an immediate code smell as the web layer should have only had a dependency on the business or domain layer. having this kind of overview of your project is very useful and allows you to get an understanding that you wouldn't necessarily get from looking on a class-by-class basis.
as well as looking at the whole solution, you can drill down into projects and classes.
code rules and code queries
select rule > viewexplorerpanel from the toolbar, and this will bring up a panel showing you an analysis based on predefined queries. these cover a range of things, such as code quality , architecture and layering and even object oriented design . each rule is displayed with an indicator of "ok," "warning," or "critical." each rule violation is explained and double-clicking on the rule will open a side panel, where you will be able to see where the violation is in the code.
by default, there are around 200 queries and rules , which, as a newbie to ndepend, should be more than enough. once you start to get more comfortable with this feature, you can write your own rules and queries using ndepend's cqlinq .
this is something i think is really useful. trend monitoring. some of the trend charts you will see by default on the ndepend dashboard are:
- lines of code.
- code rules violated and code rule violations (as a number).
- code coverage by tests.
the charts are very useful when it comes to monitoring the quality of a project over time. as a project grows, you expect the lines of code to increase, but you want the test coverage and code rule violations to stay the same. i think these charts are really useful for quantifying tech debt work. it is important to be able to show to the business the outcome of your hard work, in a sprint for example.
often, this can be difficult, so being able to show that you have reduced the number of lines of code by 5% after that big refactor, or improved test code coverage from 80% to 90%, allows you to show the value of your work in a quantifiable way. on the flip side, being able to see that a new feature increased the number of lines dramatically and also reduced the test code coverage is an early warning sign that maybe your project is heading in a direction that you do not want it to.
you can, of course, create your own custom charts as you progress with ndepend, but this is a good set to get you started.
i've been using ndepend for a few weeks now, and i am quite pleased with it. as a newbie to the scene, i was a bit daunted by the information overload because it gives you so much. but i have found that you can start using a few features and, as you get more confident, try others. this is certainly what i intend to do. hopefully, i have shown you a few that are easy to use and understand. i'm most interested in the trend charts and see them as a really useful metric. what feature/tool do you find most useful? tweet me yours @iambacon .
Published at DZone with permission of $$anonymous$$, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.