Friday, August 12, 2022

Cyclomatic Complexity Metric

 Cyclomatic Complexity Metric




Introduction to Cyclomatic Complexity (CC) metric

 

Cyclomatic Complexity in Software Testing is a testing metric used for measuring the complexity of a software program. It is a quantitative measure of independent paths in the source code of a software program. Cyclomatic complexity can be calculated by using control flow graphs or with respect to functions, modules, methods or classes within a software program.

 

Independent path is defined as a path that has at least one edge which has not been traversed before in any other paths.

 

This metric was developed by Thomas J. McCabe in 1976 and it is based on a control flow representation of the program. Control flow depicts a program as a graph which consists of Nodes and Edges.

Proposed enhancement factors, reasons for the selection of enhancement factors and modification for CC metric with each enhancement factor

                            Factor 1 - Find Type of basic control structures.

 Reason –  Complexity α Type of Basic controlstructures


When type of Basic control structure increase complexity of the program also increases.


Find Type of Basic Control Structure

Complexity of basic control structures (BCS) = Σ(Size of executable statement X Type of basic control structure value according to the amount of contribution to the complexity (CS))

Find Cyclomatic Complexity of Basic Control Structure

Cyclomatic Complexity of Basic Control Structure (CCBCS) = Cyclomatic complexity (CC) + Complexity of basic control structures (BCS)

Factor 2 - Measuring the Nesting level of control structures

Reason -   Complexity α Nesting level of control structures

When Nesting level of executable statement increase complexity of the program also increase.

This metric calculates the complexity at the nesting level considering the internal structure of the method and considers nesting to calculate the complexity of the class hierarchy

Find Complexity of Nesting level of CS

Complexity of Nesting level of Cs(CNL) = Σ(Size of executable statement (S) X Nesting level of control structure value according to the amount of contribution to the complexity (N))

Find Cyclomatic Nesting level of CS

Cyclomatic Complexity of Nesting levels of Cs(CCNL) = Cyclomatic complexity (CC) + Complexity of Nesting level of CS(CNL)


Factor 3 - Measuring the Complexity due to Inheritance

Reason         Complexity α Inheritance(I)

When inheritance count increase in program , as per that complexity of the program also increase.

This metric calculates the complexity at the method level considering the internal structure of the method and considers inheritance to calculate the complexity of the class hierarchy.

The proposed metric is validated both theoretically and empirically.

Find Complexity of Inheritance

Complexity of Inheritance (CI) =Σ(Size of executable statement (S) X Value given for the level of inheritance (I) )

Find Cyclomatic Complexity of Inheritance

Cyclomatic Complexity of Inheritance (CCI) =Cyclomatic complexity (CC) + Complexity of Inheritance (CI)

Factor 4 - Find Size of executable statement

Reason      Complexity α Size of executable statement (S)

When size of executable statement count increase in program , as per that complexity of the program also increase.

Size of a class is used to evaluate the ease of understanding of code by developers and maintainers.

Size can be measured in a variety of ways. These include counting all physical lines of

code, the number of statements, the number of blank lines, and the number of comments

lines. Lines of Code(LOC) counts all lines. Non-comment Non-blank (NCNB) is sometimes

referred to as Source Lines of Code and counts all lines that are not comments and not

blanks. Executable Statements (EXEC) is a count of executable statements regardless of

number of physical lines of code


Therefore, the proposed metrics is helping to enhanced reliability, understandability and

maintainability of executable statements of every program with high performance.

Find Complexity of Size

Complexity of size (CS) = Σ( Size of executable statement (S) )

Find Cyclomatic Complexity of Size

Cyclomatic Complexity of Size (CCS) = Cyclomatic complexity (CC) + Complexity of size (CS)

Final modified equation(FME) by considering all enhancement factors proposed


FME= ( C.Complexity of Size) + (C.Complexity of inheritance) +(C.Complexity of basic control structure) + (C.Complexity of coupling)

FME = CCS + CCI + CCBCS + CCNL


Example of calculating the complexity using final modified equation

Example 01


            Flow Graph

                

                            Cyclomatic Complexity Calculation

                                E = number of edges  = 11

                                N = number of nodes  = 11

                                P = number of nodes that have exit = 1

                                CC       =E – N + 2*P

                                            = 11 -11 + 2*1

                                            = 2

                                CC       =2


                



                                                CC = 2

Enhanced Cyclomatic Complexity for each factors.

            CCS = CC + CS = 2 + 120 =122

            CCI = CC + CI = 2 + 3 = 5

            CCBCS = CC + BCS = 2 + 19 = 21

            CCNL = CC + CNL = 2 + 0 = 2

Final Modified Equation for Cyclomatic Complexity(FME ) =CCS + CCI + CCBCS + CCNL

 

                                                                         =122 + 5 + 21 + 2

                                        FME     =150

Final Modified Equation for Software Complexity (FME) = CS + CI + BCS + NL

 

                                                                                             = 120 + 3 + 19

                                                                     FME =142

Example 02

Flow graph


Cyclomatic Complexity Calculation

 E = number of edges  = 8

N = number of nodes  = 7

                                 P = number of nodes that have exit points  = 1

 

                         CC     =E – N + 2*P

                                    = 8 -7 + 2*1

         CC     =3


CC =3

Enhanced Cyclomatic Complexity for each factors.

 CCS = CC + CS = 3 + 69 = 72

CCI = CC + CI = 3 + 4 = 7

 CCBCS = CC + BCS = 3 + 12 = 15

 CCNL = CC + CNL = 3 + 0 = 3

Final Modified Equation for Cyclomatic Complexity(FME)  = CCS + CCI + CCBCS +CCNL

                                                                          = 72 + 7 + 15 + 3

                                         FME     = 97

Final Modified Equation for Software Complexity (FME) = CS + CI + BCS + NL

                                                                                      = 69 + 4 + 12 + 0

                                                         FME = 85

Conclusion

Example

FME(cc)

FME(sc)

1

150

142

2

97

85

And when Cyclomatic complexity factors enhanced Total Cyclomatic complexity also increased

                                            References

Cyclomatic Complexity - an overview | ScienceDirect Topics

https://www.sciencedirect.com/topics/computer-science/cyclomatic-complexity

Cyclomatic Complexity | IEEE Journals & Magazine

https://ieeexplore.ieee.org/document/7725232

McCabe’s Cyclomatic Complexity with Flow Graph

https://www.guru99.com/cyclomatic-complexity.html



That's all for the Cyclomatic Complexity Metric. Thank you for reading this Post. Good Bye.












MVC Architecture