Patrick Smacchia, the creator of NDepend, provided me with a license to test it on my projects. After using it for a few days, here is a summary of what I like and dislike about NDepend.
#What is NDepend
From the product website https://www.ndepend.com:
NDepend is the only Visual Studio extension that is able to tell the developer that over the past hour, the code just written has introduced debt that would cost for example about 30 minutes should it have to be repaid later. Knowing this, the developer can fix the code before even committing it to the source control.
With NDepend code rules are C# LINQ queries that can be created and customized in a matter of seconds. These queries contain C# formulas to compute accurate technical debt estimations.
The default rule-set offers over a hundred code rules that detect a wide range of code smells including entangled code, dead-code, API breaking changes and bad OOP usage.
#What I like
- NDepend is available as a standalone application and integrates into Visual Studio, VSTS (build task and full dashboard), TeamCity, Jenkins, and SonarQube
- NDepend gives you a summary of your application in a dashboard containing key metrics, a rules execution summary, and the evolution over time of selected metrics
NDepend Dashboard
- You can easily navigate from rules to your code in one click, so you can fix them quickly.
- You can create your own rules using Code Query LINQ (CQLinq) or edit one of the 150 existing CQLinq rules. CQLinq is a linq syntax to query a model of your code enhanced with additional information such as the cyclomatic complexity, the number of lines of code, the diffs with the baseline, the usage of methods, etc. Here's an example of a custom rule that detects complex methods:
NDepend Query
- Each rule has a description that helps you understand why you should fix your code and how to fix it.
- The Debt metric helps you focus on the parts of your code that need the most attention, and estimates how long it would take to fix all violated rules.
- The Baseline feature lets you compare your code against a previous version of the application. You can detect whether you are introducing debt or reducing it. Trends are also visible in the dashboard. This is one of the most interesting features of NDepend. When starting on an existing project, the raw number of violated rules matters less than knowing whether things are getting better or worse over time.
- Thanks to the baseline, you can also detect breaking changes between two versions. Library authors will especially appreciate this feature.
- Queries run fast. Rules are evaluated in milliseconds, or a few seconds on large projects. This matters because if analysis takes too long, you will stop using NDepend daily and your code quality can drift over time.
- Great documentation
#What I don't like
- NDepend cannot fix your code automatically. Some rules, such as correcting bad naming or changing method visibility, are straightforward to fix. As a developer, I prefer using a Roslyn analyzer, ReSharper, or JustCode for these kinds of rules, since they can apply fixes in a single click.
- NDepend tells you which method or class violates a rule, but it does not show which specific lines of code are affected. For instance, the rule "Boxing/unboxing should be avoided" does not highlight the offending statement.
- By default, there are too many warnings for my taste. Some rules are not relevant to every codebase and depend on your coding style or goals. You will need to edit or disable rules, otherwise you will see hundreds of warnings. If you are adopting NDepend on an existing project, expect to spend some time tuning the rules to your needs.
#Conclusion
NDepend is a great product. It lets you query your code and ensure it matches your rules, keeping your application maintainable over time. It integrates well into the .NET ecosystem (Visual Studio, VSTS, Jenkins, etc.). While Roslyn analyzers also enable code analysis, NDepend goes further. Anyone can write a CQLinq query in minutes, which is not the case for a Roslyn analyzer. NDepend can also compare your code across versions to ensure you are not increasing technical debt. Finally, NDepend aggregates data from source code, binaries, and code coverage, enabling very advanced queries.
You can download the trial version of NDepend on the official site: https://www.ndepend.com
Do you have a question or a suggestion about this post? Contact me!