Background

A month ago, Patrick Smacchia a C# MVP and the author of Ndepend reached out to me to have a look at his product: NDepend and asked me to share my thoughts on it. I must say I felt very happy when Patrick contacted me to let me try this and also giving me the honor to write about the great tool he developed. I have never heard of NDepend before till I researched more about it since last month. Patrick offered me a PRO license to explore the features of NDepend. After learning more about this awesome product I must say I was fortunate enough to explore this tool as it gives me solution for many problems I face in my daily work as a programmer.

Coming to the product itself NDepend is a static code analysis tool which analyzes .Net code and gives insights on technical debt, Code metrics, Test coverage and much more. We all know that in software industry we try to design projects by coming up with architecture which will reduce the technical debt of the product as it evolves. However, in many times we keep neglecting to fix the technical debt as we develop new features which becomes a big set back to maintain the code and building new features on top of it. How about you know ahead of time the technical debt introduced to the product by your code at each check in to the repository with the details needed to fix those debt and manpower required to complete that effort? NDepend does exactly this for you and it does more than just giving estimation of technical debt. Let’s look at the features NDepend has for us.

Installation

Installation of Ndepend is quite simple. Go to the downloads page and get the trail version. Unzip the files to a folder and you are good to go. Once downloaded you can run the Ndepend Visual studio extension installer which installs the extension to the respective version of Visual studio. NDepend supports all versions of Visual studio to the latest 2019.

However, I faced problems installing NDepend from my workplace. I think it has to do with Ndepend website TLS protocol.

Analyze Assemblies

Once the extension has been successfully installed to the visual studio. Open any existing projects on which you want to use NDepend.  On the right bottom you should see a white circle. Click on that and add the existing project to NDepend. It should automatically show all the assemblies of the project selected. You can remove assemblies which you don’t want to analyze or select all. By default, it removes the test assemblies.

Click on analyze, NDepend will take a minute to analyze the assemblies against set of predefined rules and build a report showing a dashboard which contains lot of information regarding code metrics, quality gates, dependency graphs and much more. It was quite surprised by the speed the tool took to analyze and build the report with so much information in less than a minute. This information may be overwhelming to a person who did not encounter these metrics before, but once you start learning more about these metrics you will love this tool.

Diagrams

NDepend generates various diagrams for the assemblies analyzed. First of them is Dependency graph which can be used to drill down to look if there are any circular dependencies in the solution and how these assemblies are linked with one another. It also has option to filter out the third-party assemblies and use the application specific namespaces and classes. You can highlight an assembly and it would show you the links concerning to it. You can easily highlight design problems by looking at the dependency graph.

In case the dependency graph is big, you also have the option of viewing in form of a dependency structure matrix which is less intuitive than the graph but very scalable and is used to identify spot structural patterns and dependency cycles easily. If there is a dependency cycle, then a red box is displayed in the matrix.

It also provides us with an interactive heat map having rectangles of different sizes with different colors. This heat map is configurable with the code metrics you want to see. In the following diagram area of a rectangle is proportional to # lines of code (LOC) of the corresponding method. The color of a rectangle depends on its method value for the metric Cyclomatic Complexity (CC). Rectangles without such value remain gray.

Another very useful diagram produced by NDepend is Abstractness vs. Instability. If an assembly is very stable which means lots of folks depend on it and it’s not extensible then you’re in the lower-left quadrant of the chart in the well-named Zone of Pain. If an assembly is very abstract, very extensible, but no one depends on it (it’s not really being used) then it moves towards the Zone of Uselessness. We should always target our design such that we move away from the Zone of Pain. In addition to it the tool also provides with trend charts which allow us to visually see the progress of the metric with the baseline over the months.

Application Metrics

NDepend tool analyzes the assemblies and provides us with insights into our application metrics. It validates the assemblies with a set of predefined rules, identifies issues and categorizes them into categories such as critical, High etc. It also provides us with a dozen of default Quality Gates which are continuously checking measures such as overall Code Coverage by tests or extra Technical Debt since baseline. By integrating these quality gates into the build process, we can prevent crappy code entering the source control and not creating technical debt.

In a real-world code base scenario using NDepend can likely provide us with hundreds and thousand of issues. Stopping work to fix all these issues is unproductive and costly, NDepend provides a baseline which can be defined for a code base thereby any recent code smells which occur after baseline should be fixed before committing to source control. This increases code quality with no extra cost.

The code rules which NDepend use to validate the assembly are very helpful in understanding the concepts of object-oriented design. It tells you when a method should be declared static, warns if the namespace and the type have the same name etc. Some of the rules also check for dead methods which are not used in the code anywhere. They are rules to check the number of lines of code each method should have. Some of the rules are also based on baseline such as API breaking changes by removing any public methods. All these rules can be customized according to the needs of you and your organization.

Code Query Linq

The default rules NDepend uses CQLinq Syntax. This is a feature NDepend has come up and it is so easy to use. Adding a new customized rule is much easier. CQLinq also comes with intellisense and tooltip to help. CQLinq is fast and is designed to run hundreds of queries per seconds against a large real-world code base. NDepend has good documentation and lot of screencasts videos which walks over each of its feature, check them out which makes our life much easier.

You can also view the source code for the existing rule. The Linq syntax makes it easier to understand, for example the below rule detects any method having more than 35 lines of code and triggers a warning.

Integration

As mentioned earlier NDepend integrates with all versions of Visual studio. It can be easily integrated to Continuous integration for any pipeline repository. The marketplace already has extensions available on AzureDevops/TFS ready to be integrated. NDepend also shows automated interactive dashboard reports on Azure Devops once integrated for the pipeline. By integrating NDepend task in continuous integration process the baseline for the repository can be updated with recent release. NDepend supports all .Net profiles including the most recent release .Net Core 3.0

Wrap up

Whenever you think of designing a project it not just the initial phase but its an ongoing effort till the completion and maintainability phase. There is so much a little planning up front can do to avoid technical debt. Refactoring the code is much more valuable than pre-planning does and having NDepend which can tell you what to refactor and how much effort is required up front is a huge advantage for architects and developers. By integrating NDepend into continuous build process code quality gets improved and it becomes more maintainable. The reports that NDepend generates can give executives more control on costs and risks.

One of the drawbacks of NDepend is the learning curve required for a developer who doesn’t have the prior knowledge on the metrics the report gives and the sheer of information it provides, having said that it is too good a tool not to give a try and its highly recommended. Go Checkout NDepend.

#NDepend #CodeAnalysis #TechnicalDebt #Csharp 

 

Last modified: September 30, 2019

Author

Comments

Write a Reply or Comment

Your email address will not be published.