What is Software Metric?

Measurement is nothing but quantitative indication of size / dimension / capacity of an attribute of a product / process. Software metric is defined as a quantitative measure of an attribute a software system possesses with respect to Cost, Quality, Size and Schedule. Example- In this tutorial, you will learn-

What is Software Metric? What is Cyclomatic Complexity? Flow graph notation for a program: How to Calculate Cyclomatic Complexity Properties of Cyclomatic complexity: How this metric is useful for software testing? More on V (G): Tools for Cyclomatic Complexity calculation: Uses of Cyclomatic Complexity:

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. In the graph, Nodes represent processing tasks while edges represent control flow between the nodes.

Also Check:- Software Testing Tutorial: Free QA Course

Flow graph notation for a program:

Flow Graph notation for a program defines several nodes connected through the edges. Below are Flow diagrams for statements like if-else, While, until and normal sequence of flow.

How to Calculate Cyclomatic Complexity

Mathematical representation: Mathematically, it is set of independent paths through the graph diagram. The Code complexity of the program can be defined using the formula – Where, E – Number of edges N – Number of Nodes Where P = Number of predicate nodes (node that contains condition) Example – Flow graph for this program will be

Computing mathematically,

	V(G) = 9 – 7 + 2 = 4

	V(G) = 3 + 1 = 4 (Condition nodes are 1,2 and 3 nodes)

	Basis Set – A set of possible execution path of a program

	1, 7

	1, 2, 6, 1, 7

	1, 2, 3, 4, 5, 2, 6, 1, 7

	1, 2, 3, 5, 2, 6, 1, 7

Properties of Cyclomatic complexity:

Following are the properties of Cyclomatic complexity:

	V (G) is the maximum number of independent paths in the graph

	V (G) >=1

	G will have one path if V (G) = 1

	Minimize complexity to 10

How this metric is useful for software testing?

Basis Path testing is one of White box technique and it guarantees to execute atleast one statement during testing. It checks each linearly independent path through the program, which means number test cases, will be equivalent to the cyclomatic complexity of the program. This metric is useful because of properties of Cyclomatic complexity (M) –

	M can be number of test cases to achieve branch coverage (Upper Bound)

	M can be number of paths through the graphs. (Lower Bound)

Consider this example – Cyclomatic Complexity for this program will be 8-7+2=3. As complexity has calculated as 3, three test cases are necessary to the complete path coverage for the above example.

Steps to be followed:

The following steps should be followed for computing Cyclomatic complexity and test cases design. Step 1 – Construction of graph with nodes and edges from the code Step 2 – Identification of independent paths Step 3 – Cyclomatic Complexity Calculation Step 4 – Design of Test Cases Once the basic set is formed, TEST CASES should be written to execute all the paths.

More on V (G):

Cyclomatic complexity can be calculated manually if the program is small. Automated tools need to be used if the program is very complex as this involves more flow graphs. Based on complexity number, team can conclude on the actions that need to be taken for measure. Following table gives overview on the complexity number and corresponding meaning of v (G):

Tools for Cyclomatic Complexity calculation:

Many tools are available for determining the complexity of the application. Some complexity calculation tools are used for specific technologies. Complexity can be found by the number of decision points in a program. The decision points are if, for, for-each, while, do, catch, case statements in a source code. Cost and Effort is less Cost and effort is Medium Cost and Effort are high Examples of tools are

OCLint – Static code analyzer for C and Related Languages Reflector Add In – Code metrics for .NET assemblies

GMetrics – Find metrics in Java related applications

Uses of Cyclomatic Complexity:

Cyclomatic Complexity can prove to be very helpful in

	Helps developers and testers to determine independent path executions

	Developers can assure that all the paths have been tested atleast once

	Helps us to focus more on the uncovered paths

	Improve code coverage in Software Engineering

	Evaluate the risk associated with the application or program

	Using these metrics early in the cycle reduces more risk of the program

Conclusion:

Cyclomatic Complexity is software metric useful for structured or White Box Testing. It is mainly used to evaluate complexity of a program. If the decision points are more, then complexity of the program is more. If program has high complexity number, then probability of error is high with increased time for maintenance and trouble shoot. Also Check:- Black Box Testing Vs. White Box Testing: Key Differences