As a software developer, you may have come across a situation where you need to measure the quality the source code you have at hand. How well is the code written ? is it easily maintainable ? and similar important questions. Obviously, you don’t want to go through each line of code in the whole project to determine these. Because it will take time and you might be inaccurate. Instead, you want to analyse the source code in a fast and accurate way, and most importantly, have detailed, precise and measurable metrics about the code. Which you will easily use to judge the source code you have at hand. This is possible with static analysis tools like NDepend.
I’ve started using NDepend about a month ago, and for code analysis, it is really efficient. NDepend is simply a static analysis tool for .Net developers. Ndepend is available as a Visualstudio extension and can also be integrated to Visualstudio team services. Using this tool permits you to have a deep insight of your source code. Today, we will talk about several aspects of this tool. Which include the metrics it provides to its user and how users can get a maximum insight about their code from it.
Code Insight with NDepend Static Analysis Tool
Ndepend is easy to get started with, it is available as a visualstudio extension as I mentioned before. Installation instructions can be found here. The video present on their website is about installing an older version of the extension. So , you should follow the documentation instead.
Once you have installed it, the NDepend menu will be available on Visualstudio and from there you can create a project. After running code analysis on your project, you will have a detailed report of the analysis made on your source code. Ndepend produces a detailed report of your code also in a web page where you can visualize it. Here is a screenshot of the dashboard. I made it from a project I actually work on.
The report produced on the web page is similar to this, but they are not identical. Ndepend provides you a dependency diagram, where you can find details about the dependencies your project have and their relationships. You also have the ability to visualize the application code metrics with the dashboard, which provides a quick view of these metrics. After creation of analysis report, you have a detailed tree map of your source code. You can have detailed view of how the tree map provides more insight to your source code here.
You also have several other graphical representations of different metrics such as code coverage and depth.Below you can see a dependency graph. It is a very simple representation since the project has a few dependencies only.
Code Quality And Rules
Having a measurable representation of the quality of your code is great, and NDepend has features for this purpose. It uses quality gates to determine code quality. as mentioned on the Ndepend website, A Quality Gate is a check on a code quality fact. Ndepend provides several code quality gates available for you. Quality gates can be passed or failed, or a warning result is output. In case you have specific quality checks you want your code to pass through, Ndepend allows you to create your own quality gates or customize existing ones.
On the other side, you have rules. Rules are different from quality gates, though they have several similarities. As stated in the NDepend documentation; A Quality Gate outputs a status (Pass, Warn, Fail). Whereas, A Rule outputs issues. Defining rules is done with CQLinq. This is my favorite feature. CQLinq is similar to Linq and it is used to query your code and define rules.
Using CQLinq to query your code is simple. But in case you want to define your queries as a rule, you will need to prefix your query with warnif count. Here are few examples of queries.
This is a simple query to select every class in my project :
1 2 | from t in JustMyCode.Types where t.IsClass select t |
Another simple example is a query to get every method with more than 5 lines of code
1 2 | from t in JustMyCode.Methods where t.NbLinesOfCode > 5 select t |
To define a rules from the query above here is what we do
1 2 | warnif count > 0 from t in JustMyCode.Methods where t.NbLinesOfCode > 5 select t |
Conclusion
You will have several advantages if you use static code analytics tools. Ndepend provides large variety of options and is very easy to use and get started with.
Follow me on social media and stay updated Follow me on social media and stay updatedIf you find this article useful, please follow me on Twitter, Github, Linkedin, or like my Facebook page to stay updated.