Appendix B C API Code Samples

prevnext

Report Cyclomatic Complexity of Functions


Description

This example reports the Cyclomatic Complexity Metrics for each function. There are three complexity metrics available: Cyclomatic, Modified Cyclomatic, and Strict Cyclomatic. Refer to udbMetricEntity for definitions of each metric.

Sample Code

 1	 static void reportFunctionCyclomaticComplexity() {
 2	     UdbEntity  *ents;        
 3	     int        entsSize;    
 4	     int       i;
 5	 
 6	    udbListEntity(&ents, &entsSize);
 7	    udbListEntityFilter (ents, 
 8	                         udbKindParse("function ~unresolved  ~unknown"), 
 9	                         &ents, &entsSize);
 10	    printf ("\nFunction (Cyclomatic | Modified  | Strict )\n");
 11	    for (i=0; i<entsSize; i++) {
 12	       printf ("  %s (%d | %d | %d) \n",
 13	              udbEntityNameLong(ents[i]),
 14	              udbMetricEntity(ents[i], Udb_cMetricCyclomatic),
 15	              udbMetricEntity(ents[i], Udb_cMetricCyclomaticModified),
 16	              udbMetricEntity(ents[i], Udb_cMetricCyclomaticStrict));
 17	    }
 18	    udbListEntityFree(ents);
 19	 }

Explanation of Sample Code

lines 2-3: Declare variables for the list of entities and the list size. In this case, the list will be filtered to include only functions.

line 5: Get the list of all entities.

lines 6-8: Filter the list of entities to contain only resolved functions, replacing the original list with the new list of function entities.

line 9: Print a header line. The report will include the function name, followed by the straight Cyclomatic Complexity Metric, the Modified Cyclomatic Complexity, then the Strict Cyclomatic Complexity Metric.

line 10: Loop through the list of entities, in this case all (known, resolved) functions.

lines 11-15. Print the complexity metrics. For each function, print the (long) function name, and the three complexity metrics.

line 17: Free the list of function entities.

Sample Output

 Function (Cyclomatic | Modified  | Strict )
   Trace (0 | 0 | 0)
   CBmp::CBmp (1 | 1 | 1)
   CBmp::~CBmp (1 | 1 | 1)
   CBmp::CreateCopy (8 | 6 | 10)
   CBmp::Create (1 | 1 | 1)
   CBmp::SetGrayPalette (2 | 2 | 2)
   CBmp::SetPalette (1 | 1 | 1)
   CBmp::SetPaletteEntry (1 | 1 | 1)
   CBmp::SetAlphaChannel (3 | 3 | 3)
   CBmp::GetWidth (1 | 1 | 1)
   CBmp::GetHeight (1 | 1 | 1)
   CBmp::GetNumColors (2 | 2 | 2)
   CBmp::GetBitsPerPixel (1 | 1 | 1)
   CBmp::HasAlpha (1 | 1 | 1)
   CBmp::GetPalette (1 | 1 | 1)
   CBmp::GetLineArray (1 | 1 | 1)
   CBmp::initLocals (2 | 2 | 2)
   CBmp::create32BPPCopy (8 | 8 | 8)
   CBmp::create8BPPCopy (5 | 5 | 5)
   CBmp::create1BPPCopy (4 | 4 | 6)
   SetRGBAPixel (0 | 0 | 0)

prevnext


Scientific Toolworks, Inc.
http://www.scitools.com
Voice: (802) 763-2995
Fax: (802) 763-3066
support@scitools.com
sales@scitools.com