Cyclomatic Complexity in .NET Core
Cyclomatic complexity is a simple code metric, commonly used to identify complex methods which are difficult to maintain and therefore good candidates for refactoring. It is one of the five code metrics built into Visual Studio 2017.
Surprisingly, the code metrics feature in Visual Studio 2017 (version 15.7) isn't available for .NET Core and .NET Standard projects. If you try to generate code metrics for an unsupported project type, the command won't be available.
If you generate them for the full solution instead, unsupported projects will be skipped with the following error:
One or more projects were skipped. Code metrics are available only for C#, Visual Basic, and C++/CLI projects that are not Web Site projects.
Let's look at third party extensions which you can use instead.
NDepend
NDepend is probably the most advanced static code analysis tool for .NET, therefore it was my first choice when looking for alternatives. Of course, cyclomatic complexity is one of many supported code metrics. It will be calculated as part of the default analysis configuration.
The cyclomatic complexity value is also included in calculations for default project rules, such as the Avoid methods too big, too complex in the Code Smells group. Thanks to its most recognizable Code Query over LINQ feature, you can easily include it in your own rules and queries as well.
ReSharper
ReSharper used to be the must-have extension for many .NET developers until the introduction of Roslyn compiler services with Visual Studio 2015 when many of its features were built directly into Visual Studio or provided by other free extensions.
Although there are many code inspections built into ReSharper, cyclomatic complexity isn't one of them. However, you can install a free plugin to add the functionality. After installing it (and restarting Visual Studio), the metrics value will be displayed inside the tooltip for each method name.
The threshold value for triggering a warning can be configured in ReSharper Options for Code Inspection.
CodeRush
The other large Visual Studio extension from the era before Roslyn was CodeRush. Unlike ReSharper, it was rewritten since then to take advantage of code analysis provided by Roslyn. In its latest version, cyclomatic complexity is one of the three code metrics available. You can choose to display the value for one of them in front of every method.
You can globally enable or disable the display of code metrics values in CodeRush Quick Setup options window.
CodeMaid
I haven't heard about CodeMaid until I started searching for extensions with support for cyclomatic complexity. It's a free open source extension for cleaning up and reorganizing code. Cyclometric complexity is displayed in the CodeMaid Spade treeview for code navigation.
In CodeMaid options window, warning and alert threshold values for cyclomatic complexity can be set. Based on those, the value in CodeMain Spade will be rendered in a different color.
Continuous Integration
So far, I have described how cyclomatic complexity values are presented in Visual Studio during development. If you would like to calculate the code metrics as part of continuous integration on the build server, there's even less choice. Only two extensions have command line support:
NDepend includes a fully featured command line version which can be used from any build server. Instructions are provided for using it with many of them. In a previous blogpost, I explored its integration with TeamCity.
ReSharper has a free command line tool for running code inspections on a build server. Support for extensions is incomplete and poorly documented. However, it seems that the cyclomatic complexity plugin can be used with it.
Summary
Let's conclude with an overview of available extensions and their support for cyclomatic complexity.
Extension | .NET framework | .NET Standard | .NET Core | Continuous integration |
---|---|---|---|---|
Visual Studio 2017 | Yes | No | No | No |
NDepend | Yes | Yes | Yes | Yes |
ReSharper | Yes | Yes | Yes | Yes |
CodeRush | Yes | Yes | Yes | No |
CodeMaid | Yes | Yes | Yes | No |
If you're looking for a solution to use inside Visual Studio 2017 during development, your options are ReSharper, CodeRush and CodeMaid. If you aren't using any of them yet, start with CodeMaid which is the only one available for free. Otherwise, just enable the feature in the extension you're already using. It won't cost you anything extra and will cause the smallest performance hit.
In continuous integration scenarios, your best choice is NDepend. It's not cheap, but it will be worth the price if you take advantage of the many features it provides. If you're really looking only for cyclomatic complexity, you could try getting ReSharper command line tool to work, but don't expect it to add a lot of value to your build process.