Take advantage of the powerful static code analysis tool to identify and understand issues in your .Net application’s code Credit: Thinkstock Assessing the quality of an application’s code is often a subjective process. This is why we turn to code metrics — quantitative measurements that provide valuable insights into our application’s code. Developers can take advantage of code metrics to understand the quality of the code, get an idea of the potential problems, and identify which types and methods need to be refactored to improve the quality. Static code analysis tools are used to measure the quality of code in an application without having to execute the application. There are many static code analysis tools available for .Net. These include FxCop, StyleCop, ReSharper, CodeIt.Right, NDepend, etc. This article presents a discussion of how we can use NDepend to visualize code quality and adopt measures to improve it. What is NDepend and why use it? NDepend is a static code analyzer that integrates nicely with Visual Studio and provides valuable metrics for determining code quality. NDepend displays these metrics in the form of lists, matrixes, graphs, tree maps, and charts. Along with these metrics, NDepend can analyze your code and report violations against a large database of rules. NDepend saves the results of each analysis, so you can compare the results after you’ve improved upon the code coverage or code quality. It can be run both as a standalone application and as an extension in Visual Studio. And you can take advantage of the NDepend Azure DevOps extension to integrate NDepend with your projects in Azure. The following are some of the striking features of NDepend: Large set of code rules to check for compliance Many charts and diagrams for viewing code metrics Integrates nicely with Visual Studio 2010, 2012, 2013, 2015, and 2017 Enables you to explore the dependencies between modules Provides CQLinq (Code Query over LINQ) to customize analysis Measures code coverage Identifies difficult-to-maintain code and estimates technical debt Creates trend charts Getting started with NDepend To get started using NDepend, you’ll need to download and install a copy of it. You can download a trial copy of NDepend from here. It will be packaged as a .zip file. Once you have unzipped the NDepend installer .zip file, you will find the following files inside: NDepend.Console — used for integration with the build process NDepend.PowerTools — a collection of open-source static analyzers NDepend.VisualStudioExtension.Installer — installs the Visual Studio Extension for NDepend VisualNDepend — a GUI client for NDepend You can install the Visual Studio extension of NDepend in one of the following two ways: Run NDepend.VisualStudioExtension.Installer Run VisualNDepend and select “Install Visual Studio Extension” Once Visual Studio Extension for NDepend is installed, you can leverage the metrics, rules, reports, graphs, analyzer results, and tools from within the Visual Studio IDE. You can also take advantage of Visual NDepend to analyze your solutions and projects without having to of launch Visual Studio. The Visual Studio Extension for NDepend is available for Visual Studio 2010 through Visual Studio 2017. Note that I am using it with Visual Studio 2017. Analyzing source code with NDepend To start analyzing your source code, you can click on “Analyze VS solutions and VS projects.” The other options available are “Analyze .Net assemblies in folder,” “Analyze a set of .Net assemblies,” and “Compare 2 versions of a code base.” That last option lets you compare two builds — a nice feature! For the sake of simplicity, I will be using the source code from one of my recent articles here (“How to use Lamar in ASP.Net Core”). Once you click on “Analyze VS solutions and VS projects,” a new window will be displayed as shown in Figure 1 below. IDG Figure 1: Select one or more Visual Studio solutions or projects to be analyzed. You can now click on “Browse” and specify the solution file of the project you would like NDepend to analyze. IDG Figure 2: Selecting the VS solution to analyze. Lastly, click on “Analyze a single .Net assembly” to start the analysis as shown in Figure 3 below. IDG Figure 3: Analyzing a .Net assembly. This will run a code analysis on the selected project. When the analysis is complete, NDepend will display a window with the following options: View NDepend Dashboard Show NDepend Interactive Graph Browser NDepend Code Rules Close the dialog Let’s choose to view the NDepend dashboard. Here’s what the output looks like: IDG Figure 4: The NDepend dashboard. NDepend shows you the lines of code, information on types, debt, comments in the source code, coverage information, method complexity, quality gates and the rules that have been violated and other issues. NDepend features Dependency graph and dependency matrix NDepend also shows you a dependency graph and dependency matrix of your code. While the former provides a graphical view of the dependencies in your project, the latter provides a tabular view of your code organized by dependencies on namespaces and types. Trend charts Trend charts are available in the dashboard. These charts provide a preview on how the quality of the project’s source code has been changing over time. You can also create your own custom trend charts. IDG Figure 5: Trend charts in NDepend. Cyclomatic complexity Cyclomatic complexity is a quantitative measure of the linearly independent paths in source code that can help you understand the complexity of your program and improve code coverage. You can take advantage of NDepend to analyze the cyclomatic complexity in your application’s code. You can read more about cyclomatic complexity in this article. IDG Figure 6: Measuring cyclomatic complexity in NDepend. Querying using CQLinq CQLinq is one of the most striking features of NDepend. CQLinq enables you to query .Net code using LINQ. You can take advantage of CQLinq to query code debt, issues, rules, and quality gates. IDG Figure 7: NDepend’s CQLinq in action! Let’s look at an example. The following CQLinq query displays the method names and cyclomatic complexity of non-abstract methods that have a cyclomatic complexity greater than 20, ordered by descending order of the value of cyclomatic complexity. from m in Application.Methods where m.CyclomaticComplexity >= 20 && !m.IsAbstract orderby m.CyclomaticComplexity descending select new { m, m.CyclomaticComplexity } Here is another example of a CQLinq query. This one when executed will display the names of the methods that have more than 100 lines of code. from m in Methods where m.NbLinesOfCode > 100 select m To learn more about CQLinq, you can refer to the NDepend documentation here. NDepend is a static code analysis tool that can give you valuable insights into your application’s code. You can run NDepend as a standalone application or integrated in Visual Studio. Although NDepend is not free, it is reasonably priced considering its powerful capabilities, and it is available for all recent versions of Visual Studio. If you want a great resource for learning NDepend, I highly recommend the Pluralsight course “Practical NDepend” by Eric Dietrich. Related content analysis Azure AI Foundry tools for changes in AI applications Microsoft’s launch of Azure AI Foundry at Ignite 2024 signals a welcome shift from chatbots to agents and to using AI for business process automation. By Simon Bisson Nov 20, 2024 7 mins Microsoft Azure Generative AI Development Tools news Microsoft rebrands Azure AI Studio to Azure AI Foundry The toolkit for building generative AI applications has been packaged with new updates to form the Azure AI Foundry service. By Anirban Ghoshal Nov 19, 2024 4 mins Microsoft Azure Generative AI Development Tools feature 14 great preprocessors for developers who love to code Sometimes it seems like the rules of programming are designed to make coding a chore. Here are 14 ways preprocessors can help make software development fun again. By Peter Wayner Nov 18, 2024 10 mins Development Tools Software Development news JetBrains IDEs ease debugging for Kubernetes apps Version 2024.3 updates to IntelliJ, PyCharm, WebStorm, and other JetBrains IDEs streamline remote debugging of Kubernetes microservices and much more. By Paul Krill Nov 14, 2024 3 mins Integrated Development Environments Java Python Resources Videos