Cyclomatic complexity .net tool
The above piece of code has a cyclomatic complexity of exactly 1. Because there is only one way to pass through the code, top to bottom. Now our code has a cyclomatic complexity of 2. The above example I included intentionally to illustrate that the cyclomatic complexity does not necessary depend on the unique outcomes, but instead the path any particular execution of the code can take. In the above code, the cyclomatic complexity is 3, even though two of the paths return false.
The most obvious next question is, why does it matter? 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. 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. 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. So far, I have described how cyclomatic complexity values are presented in Visual Studio during development. The bottom line is that a high complexity number means greater probability of errors with increased time to maintain and troubleshoot.
Take a closer look at any functions that have a high complexity and decide if they should be refactored to make them less complex. McCabe, T. Watson, A. Rosenberg, L. Skip to main content. This browser is no longer supported. Download Microsoft Edge More info. Contents Exit focus mode. Please rate your experience Yes No. Any additional feedback? A node represents a logic branch point and an edge represents a line between nodes.
You can learn more about code metrics at Measure complexity of managed code. It is safe to suppress a warning from this rule if the complexity cannot easily be reduced and the method is easy to understand, test, and maintain. In particular, a method that contains a large switch Select in Visual Basic statement is a candidate for exclusion. The risk of destabilizing the code base late in the development cycle or introducing an unexpected change in run-time behavior in previously shipped code might outweigh the maintainability benefits of refactoring the code.