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.

No code metrics for .NET Core projects in Visual Studio 2017

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.

Cyclomatic complexity in NDepend

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.

Tooltip with cyclomatic complexity value provided by ReSharper

The threshold value for triggering a warning can be configured in ReSharper Options for Code Inspection.

Cyclomatic complexity threshold configuration in ReSharper

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.

Code metric display and options in CodeRush

You can globally enable or disable the display of code metrics values in CodeRush Quick Setup options window.

Code metrics display toggle in CodeRush options

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.

Cyclomatic complexity value in CodeMaid Spade treeview

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.

Cyclomatic complexity value in CodeMaid Spade treeview

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:

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.

Get notified when a new blog post is published (usually every Friday):

Copyright
Creative Commons License