![]() |
General Programming »
Macros and Add-ins »
Visual Studio .NET Addins
Intermediate
Cyclomatic Complexity ViewerBy Franz KleinA Cyclomatic complexity viewer application. |
VC6, VC7.1WinXP, ATL, WTL, VS.NET2003, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

The Cyclomatic Complexity Metric[1] "measures the amount of decision logic in a single software module"[2]. In my implementation I have taken the software module to be a function. The cyclomatic complexity "is based entirely on the structure of the software's control flow graph" [2]. The formula for calculating the cyclomatic complexity for each function using its control flow graph is:
![]()
where E and N are the number of edges and nodes in the control flow graph, respectively. Figure 1 displays the control flow graphs for the control structures found in the C++ language except for for. My understanding of the Cyclomatic Complexity Metric is that it measures how complex a function is and this determines how easy it will be to test the specific function.
|
|
|
|
| if | if/else | while |
|
|
|
|
| do | switch |
Figure 1. Control flow graphs
At last after nearly five years of working on this add-in, I have finally released it. Why five years you may ask? Well after finding and implementing an algorithm to display directed graphs, improving the program, then discovering Graphviz, losing all the source code and starting from the beginning again, it has taken me that long. For the VC++ 6 version of the add-in, I have made use of Norm Almond's CLabel class. In the VS.NET 2003 version, I made use of Rashid Thadha's ATL version of the CLabel class.
After downloading the CCMetricViewer_DLL.zip file:
After downloading the VC6_DLL.zip file:
![]()
This add-in is very buggy at the moment. It has a number of problems that still need to be fixed. Below is a list of the problems:
if (A) D;
or
if (B) {C};
However it will be able to parse source code in the form of:
if (A)
D;
or
if (B)
{C}
Both buttons are used to calculate a cyclomatic complexity metric, however the matrices calculated are those of:
respectively.
The first button is used when you want to find out the cyclomatic complexity metric of a specific function. Just select the specific function and click the button. As shown in Figure 2(a) and 2(b), a dialog with the name of the function and its specific metric is displayed. If the metric value is greater than 10 the value is displayed in red, otherwise it is displayed in black.
![]() |
![]() |
| Figure 2(a). Less than 10 | Figure 2(b). Greater than 10 |
Figure 2. Metric for a specific selected function
Figure 3. All the matrices of the loaded Solution (Click here to enlarge)
In addition to the list, the dialog has a number of controls which can be used to manipulate the data.
Figure 4 shows the control that is used to set the metric level for comparisons. Initially it is set to the default value of 10. A number of people have recommended that a value of 10 is taken as the default level against which to compare cyclomatic complexity matrices of functions. Any function with a metric value higher than 10 is considered to be too complex and needs to be broken up into a number of smaller functions. The table below gives some idea:
| 1-10 | - | a simple program |
| 11-20 | - | more complex |
| 21-50 | - | complex |
| > 50 | - | untestable[3] |
All functions with a metric value higher than the currently set metric level will be highlighted in the list. Figure 5 shows the results of applying the add-in to a currently loaded Solution and then setting the metric level to 5.
|
Figure 4. Default metric level of 10 |
Figure 5. Metric level set at 5 (Click here to enlarge) |
Figure 6 shows the 'Export' button which can be used to have the results, as displayed in the list, exported to a pre-named XML file.
Figure 6. Export results to an XML file
The XML file has the following format:
<solution name="">
<project name="">
<class name="">
<file name="">
<function name="">
<path name=""></path>
<metric></metric>
</function>
</file>
</class>
</project>
<project name="">
...
</project>
...
</solution>
The VC++ 6 version of the add-in can only determine the cyclomatic complexity metric for a single function. Select the function by highlighting the code for the specific function. Then click on the Add-in button and a dialog as in Figure 7 will appear displaying the metric value. If the metric value is greater than 10 the value is displayed in red, otherwise it is displayed in black.
Figure 7. Metric value of a specific selected function
I will gladly appreciate any comments, suggestions, especially if it helps me fix the problems as stated in the Warning section.
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 30 Jun 2005 Editor: Smitha Vijayan |
Copyright 2005 by Franz Klein Everything else Copyright © CodeProject, 1999-2009 Web22 | Advertise on the Code Project |