Over a million developers have joined DZone.

Find Code Cyclomatic Complexity with NDepend

DZone's Guide to

Find Code Cyclomatic Complexity with NDepend

Free Resource

Cyclomatic Complexity or also known as ‘Conditional Complexity’ is measurement of complexity of a program code. The cyclomatic complexity is calculated using the control flow graph of the program, so this is based on the graph theory.

Code Cyclomatic Complexity = Number of graph edges – Numer of Graph Nodes + Number of connected components

With NDepend in hands, with only one CQL query, you can find all the methods which has cyclomatic complexity higher than 15 (methods hard to understand/maintain) or 30 (extremely complex methods).

From NDepend metrics site:

Cyclomatic Complexity (CC): (defined for types, methods) (Only available for C# code, a VB.NET version is currently under development) Cyclomatic complexity is a popular procedural software metric equal to the number of decisions that can be taken in a procedure. Concretely, in C# the CC of a method is 1 + {the number of following expressions found in the body of the method}:
if | while | for | foreach | case | default | continue | goto | && | || | catch | ternary operator ?: | ??
Following expressions are not counted for CC computation:
else | do | switch | try | using | throw | finally | return | object creation | method call | field access
Adapted to the OO world, this metric is defined both on methods and classes/structures (as the sum of its methods CC). Notice that the CC of an anonymous method is not counted when computing the CC of its outer method.
Recommendations: Methods where CC is higher than 15 are hard to understand and maintain. Methods where CC is higher than 30 are extremely complex and should be split in smaller methods (except if they are automatically generated by a tool).

Link: http://www.ndepend.com/Metrics.aspx#CC

Let’s see the cyclomatic complexity of the following code snippet:

The cyclomatic complexity of this code snippet is: 6

 public void Test(int x)
if (x > 1)
if (x > 1 && x < 10)
else if (x < 1 && x > -50)

= 1 for the method
= 1 for if (x > 1)
= 2 for if (x > 1 && x < 10)
= 2 for else if (x < 1 && x > –50)

It seems easy to calculate the cyclomatic complexity of one method, but what if you have 10.000+ methods where you want to find the most complex methods? NDepend is your friend!

If you didn’t know about NDepend, you can first read my other two blog posts about NDepend:

  1. NDepend tool – Why every developer working with Visual Studio.NET must try it!
  2. NDepend – Code Query Language (CQL)

So, if you have attached NDepend in VS.NET or you are running it using Visual NDepend, open the CQL Editor and write the following CQL code:

WHERE CyclomaticComplexity > 15


The result in the current code library I’m testing this is:

(I’m showing only few of the methods)

You can also order the results by appending ORDER BY CyclomaticComplexity DESC in the CQL query.

Besides normal Cyclomatic Complexity, with NDepend you can find IL Ciclomatic Complexity also.

I hope this was helpful post.





Published at DZone with permission of Hajan Selmani, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}