This Software metrics tutorial is divided into many parts. Please read all the parts to get a complete understanding of usge software metrics in software development.

**4. Cyclomatic Complexity metric** has be propounded by Thomas McCabe in 1976 .We explain and give here ways to calculate Cyclomatic Complexity with examples.

This metric tries to gauge the complexity of a code implementation . According to this theory , as code complexity goes up , number of defects found in that code also go up, leading to high maintenance cost.

A cyclomatic complexity value can predict about the chances of defects occurring.

It’s been observed that at HP , after finding out a high cyclomatic complexity of a 77 KLOC (77000 lines of code ) program , subsequent **refactoring (simplification to reduce cyclomatic complexity number of code**) was done . This can be done by breaking down a single huge function into many small functions .

As a result , when the program was released, a the external defect rate** came down** to only 0.31 defects per KLOC. A 125 KLOC program’s external defect rate was only 0.02 defects per KLOC .

**How to measure Cyclomatic Complexity**

Cyclomatic Complexity can be measured as follows:

If in a code , the number of nodes is N, and the number of edges is E, then cyclomatic complexity is C = E - N + 1.

However , practically , its easiest to measure Cyclomatic complexity as given below (refer MSDN , and Code Complete by Steven C. McConnell )

To calculate Cyclomatic Complexity of a function , start with 1 , then as we encounter every if , while etc , keep adding 1 with the initial 1 . Use the following chart.

**Practical Example**

To see how it is done , lets see the following code:(download here)

In the above code , to **calculate** the Cyclomatic Complexity , we start with 1 .

*Start with 1*

*Add 1 for 'if' = Cyclomatic Complexity = 2*

*Add 1 for' || ' (OR) = Cyclomatic Complexity = 3*

*Add 1 for first 'case' = Cyclomatic Complexity = 4*

*Add 1 for second 'case' = Cyclomatic Complexity = 5*

So , cyclomatic complexity of the above function is 5.

There are tools that calculate Cyclomatic Complexity and its easier than manual in case the code base is big. One such **tool** is **CCCC** for **C++ code** . See the tutorial for it in next chapter.

* -By Mala Bose . Google+*

**Metrics**- Requirement churn metrics
- Estimation Churn metrics
- Defects per KLOC
- Cyclomatic Complexity of Code metrics
- CCCC tool for calculating Cyclomatic Complexity
- Defect removal efficiency (DRE) metrics
- Fault Density metrics
- Defect Density Metrics
- Halstead Metrics

- Cause and Effect diagram
- State Transition Diagram
- Decision Tables
- Equivalence Partition
- Pairwise & Orthogonal Array Testing Tutorial
- Boundary Value

Copyright QA Friend Software Testing. All rights reserved.