Click here to Skip to main content
12,295,662 members (52,352 online)
Click here to Skip to main content
Add your own
alternative version


37 bookmarked

Cyclomatic Code Complexity Analysis for Microsoft .NET Applications

, 21 Sep 2005
Rate this:
Please Sign up or sign in to vote.
Code Complexity is a measure that provides a single ordinal number which can be compared to the complexity of other programs. It is one of the most widely accepted static software metrics and is intended to be independent of language and language format.


Cyclomatic Code Complexity was first introduced by Thomas McCabe in 1976. In 1976, Thomas McCabe published a paper arguing that code complexity is defined by its control flow. Since that time, others have identified different ways of measuring complexity (e.g. data complexity, module complexity, algorithmic complexity, call-to, call-by, etc.). Although these other methods are effective in the right context, it seems to be generally accepted that control flow is one of the most useful measurements of complexity, and high complexity scores have been shown to be a strong indicator of low reliability and frequent errors.


This measure provides a single ordinal number that can be compared to the complexity of other programs. It is one of the most widely accepted static software metrics and is intended to be independent of language and language format.

Code Complexity is a measure of the number of linearly-independent paths through a program module and is calculated by counting the number of decision points found in the code (if, else, do, while, throw, catch, return, break etc.).

Technical Specification

Cyclomatic Complexity for a software module calculated based on graph theory is based on the following equation:



  • CC = Cyclomatic Complexity
  • E = the number of edges of the graph
  • N = the number of nodes of the graph
  • p = the number of connected components

Further academic information on the specifics of this can be found here.

From a layman’s perspective the above equation can be pretty daunting to comprehend. Fortunately there is a simpler equation which is easier to understand and implement by following the guidelines shown below:

  • Start with 1 for a straight path through the routine.
  • Add 1 for each of the following keywords or their equivalent: if, while, repeat, for, and, or.
  • Add 1 for each case in a switch statement.

Let’s look at a few examples to understand how the code complexity is calculated.

Example 1

public void ProcessPages()
 while(nextPage !=true)
  if((lineCount<=linesPerPage) && (status != Status.Cancelled) && (morePages == true))

In the code above, we start with 1 for the routine, add 1 for the while loop, add 1 for the if, and add 1 for each && for a total calculated complexity of 5.

Example 2

public int getValue(int param1) 
 int value = 0;
 if (param1 == 0)
  value = 4;
  value = 0;
 return value;

In the code above, we start with 1 for the routine, add 1 for the if, and add 1 for the else for a total calculated complexity of 3.

Members that have high code complexity should be reviewed for possible refactoring.

The SEI provides the following basic risk assessment based on the value of code:

Cyclomatic Complexity Risk Evaluation
1 to 10 a simple program, without very much risk
11 to 20 a more complex program, moderate risk
21 to 50 a complex, high risk program
> 50 an un-testable program (very high risk)


There are several free tools available which help one analyze the code complexity:

  • devMetrics by Anticipating minds have a free community edition available for analyzing metrics for C# projects.
  • Reflector Add-In: Code Metrics can be used to analyze .NET assemblies and show design quality metrics. This add-in is to be used in conjunction with Lutz Roeder’s Reflector.


  • It is very easy to compute as illustrated in the example.
  • Unlike other complex measurements, it can be computed immediately in the development cycle (which makes it agile friendly).
  • It provides a good indicator of the ease of code maintenance.
  • It can help focus testing efforts.
  • It makes it easy to find complex code for formal review.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
DaveBlack4-Jan-11 9:06
memberDaveBlack4-Jan-11 9:06 
NewsUpdate link to CodeMetrics .NET Reflector Add-In Pin
Michael Freidgeim30-Jan-09 10:45
memberMichael Freidgeim30-Jan-09 10:45 
GeneralFew questions on calculating CC of program Pin
Shiva Prasad Sheela22-Apr-08 1:58
memberShiva Prasad Sheela22-Apr-08 1:58 
1) Is CC measured for just a function or complete program or application?

2)If CC is measured for application, CC value shall be more than 50 most of the times. As per chart, if CC > 50 the application is considered to be highly risky?

3) How to compute the CC of an application? Is it like adding the CC value of each function will result in CC of an application?
GeneralGreat freeware tool to display Cyclomatic Complexity Pin
Hannes Pavelka19-Mar-06 3:14
memberHannes Pavelka19-Mar-06 3:14 
GeneralRe: Great freeware tool to display Cyclomatic Complexity Pin
DSK Chakravarthy24-Nov-14 22:40
memberDSK Chakravarthy24-Nov-14 22:40 
GeneralVisual Studio .NET 2005 Pin
ssaSpot28-Sep-05 8:14
memberssaSpot28-Sep-05 8:14 
QuestionVB.NET?? Pin
Drew Berkemeyer27-Sep-05 3:30
memberDrew Berkemeyer27-Sep-05 3:30 
AnswerRe: VB.NET?? Pin
Saikalyan Prasadrao27-Sep-05 4:12
memberSaikalyan Prasadrao27-Sep-05 4:12 
AnswerRe: VB.NET?? Pin
alfalfa27-Sep-05 4:14
memberalfalfa27-Sep-05 4:14 
GeneralThanks Pin
leppie21-Sep-05 7:08
memberleppie21-Sep-05 7:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160525.2 | Last Updated 21 Sep 2005
Article Copyright 2005 by Saikalyan Akunuri
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid