Click here to Skip to main content
15,867,704 members
Articles / Desktop Programming / WTL
Article

Cyclomatic Complexity Viewer

Rate me:
Please Sign up or sign in to vote.
4.29/5 (13 votes)
30 Jun 20055 min read 107.4K   2.8K   34   15
A Cyclomatic complexity viewer application.

Metric Level set at 5

Introduction

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:

E - N + 2

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.

Image 3

Image 4

Image 5

ifif/elsewhile

Image 6

Image 7

 
doswitch 

Figure 1. Control flow graphs

Background

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.

Installing the Add-in

VS.NET 2003

After downloading the CCMetricViewer_DLL.zip file:

  1. Extract the files using the folder names.
  2. Double-click on the ReCreateCommands.reg file to register the add-in. After installation, the following buttons should appear on the toolbar: Image 8.
  3. If nothing appears, click on the Tools menu item and select Add-in Manager... from the dropdown menu. The Add-in Manager dialog will appear. Now make sure that both the boxes for the Available Add-ins and the Startup are selected. The toolbar should appear now.
  4. Also look at the View\Toolbars menu item and select Metric Viewer from the list.
  5. If still nothing appears then try devenv.exe /setup.

VC++ 6

After downloading the VC6_DLL.zip file:

  1. Extract the files using the folder names.
  2. Click on the Tools menu item and select Customize... from the dropdown menu. The Customize dialog will appear. Click on the Add-ins and Macro Files tab. Click the checkbox labeled CycloComplexViewer Add-In to enable it.
  3. The following button will appear:

    Button

Warning

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:

  1. It cannot parse source code in the form of:
    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}
  2. The list display in the dialog does not automatically update when the scroll buttons are manipulated. It only updates itself once one scrolls down the list using the vertical scrollbars.

Using the Add-in

VS.NET 2003

Both buttons are used to calculate a cyclomatic complexity metric, however the matrices calculated are those of:

respectively.

A Specific Function

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.

Image 12Image 13
Figure 2(a). Less than 10Figure 2(b). Greater than 10

Figure 2. Metric for a specific selected function

All Functions of a Solution

The second button will calculate the cyclomatic complexity matrices of all the (global and member) functions found in the currently loaded Solution. Just click the button. As shown in Figure 3, a dialog with a list control containing all the functions in the Solution and their respective matrices is displayed.

Solution's metrices

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.

Image 15

Image 16

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.

Image 17

Figure 6. Export results to an XML file

The XML file has the following format:

XML
<solution name="">
 <project name="">
  <class name="">
   <file name="">
   <function name="">
   <path name=""></path>
   <metric></metric>
   </function>
   </file>
  </class>
 </project>
 <project name="">
  ...
 </project>
 ...
</solution>

VC++ 6

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.

Image 18

Figure 7. Metric value of a specific selected function

Feedback

I will gladly appreciate any comments, suggestions, especially if it helps me fix the problems as stated in the Warning section.

References and Further Information

  1. McCabe, T., "A Complexity Measure", IEEE Transactions on Software Engineering, December 1976.
  2. Structured Testing: A Testing Methodology Using the Cyclomatic Complexity Metric
  3. Cyclomatic Complexity

History

  • 24 June 2005 - Added files for VC++ 6 version and updated article.
  • 17 June 2005 - First public release.

License

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


Written By
United Kingdom United Kingdom
I am a qualified Veterinary Surgeon who prefers treating computers with viruses than animals with viruses. I have recently completed a MEng German Informatics degree at the University of Reading with a 2:1. I also have the ISEB Foundation Certificate in Software Testing.

Currently I am umemployed and desparately looking for a job in the IT industry.

Comments and Discussions

 
Generalabout the computation of cyclomatic complexity and construction of control flow graph Pin
suraj karki29-Apr-09 23:56
suraj karki29-Apr-09 23:56 
Dear Franz klein

I am suraj.I knew about your work and findings in the field of software engineering through website codeproject.com. I am also very interested in the field of software engineering and doing the project on the topic related to it.
I downloaded both VS.net 2003 version and Vc++6 version of your project. I couldnot run the vs.net 2003 version however i can partially run the vc++ version.When i build the project in vc++, it successfully compiles all the file but at the last it gives the error like

Generating Code...
Linking...
LINK : fatal error LNK1104: cannot open file "cximage.lib"
Error executing link.exe.
CycloComplexViewer.dll - 1 error(s), 0 warning(s)


What kind of error is this? What should be done to successfully compile this project?Please help me to fix this problem.

your project can calculate the cyclomatic complexity of a program and generates the directed graph as well but that directed graph is generated in a text file. Cannot that directed graph be genearated in the GUI form and shown in separate window as in cyclomatic complexity.Except that can you generate the basis set of independent paths on the basis of generated directed graph.
I seek the answer of these questions because i am doing a project on "generation of basis set of paths for path testing(white box testing)".
If you could do whatever I have mentioned above, I will be very helpful for my project.And if possible,can you provide the documentation of your project to me for the reference purpose ?

you havenot included the "for loop" in your project. Why? doesnot it affect the computation of cyclomatic complexity.Similary, the control flow graph for "while loop" given by you and given in the book of Roger S. Pressman ,fifth edition is different? which is the correct one?Confused | :confused:


I hope that you will respone my mail and willnot make me disappointed. Frown | :(
I look forward to hearing from you very soon. Blush | :O
Thanking you
suraj
GeneralNot able to use the dll with Visual Studio 8.0 Pin
Venki259-Jul-07 22:26
Venki259-Jul-07 22:26 
GeneralMissing Code Pin
Tim Tinker13-Jan-06 5:52
Tim Tinker13-Jan-06 5:52 
GeneralRe: Missing Code Pin
Franz Klein13-Jan-06 7:08
Franz Klein13-Jan-06 7:08 
AnswerRe: Missing Code Pin
Franz Klein14-Jan-06 0:23
Franz Klein14-Jan-06 0:23 
GeneralDLL failed to load Pin
admiralh25-Jul-05 6:56
admiralh25-Jul-05 6:56 
GeneralRe: DLL failed to load Pin
Anonymous7-Jul-05 0:34
Anonymous7-Jul-05 0:34 
GeneralRe: DLL failed to load Pin
Franz Klein7-Jul-05 0:45
Franz Klein7-Jul-05 0:45 
GeneralRe: DLL failed to load Pin
admiralh27-Jul-05 7:18
admiralh27-Jul-05 7:18 
Generalthe DLL for VC7 is a debug version Pin
volatileMike1-Jul-05 5:17
volatileMike1-Jul-05 5:17 
GeneralSimplifying the parser Pin
Jose Lamas Rios19-Jun-05 16:42
Jose Lamas Rios19-Jun-05 16:42 
GeneralRe: Simplifying the parser Pin
Franz Klein24-Jun-05 3:40
Franz Klein24-Jun-05 3:40 
GeneralCannot load dll Pin
Rage17-Jun-05 1:26
professionalRage17-Jun-05 1:26 
GeneralRe: Cannot load dll Pin
Franz Klein17-Jun-05 3:31
Franz Klein17-Jun-05 3:31 
GeneralRe: Cannot load dll Pin
Brad Bruce17-Jun-05 4:45
Brad Bruce17-Jun-05 4:45 

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.