- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Mccabe's Cyclomatic Complexity: Calculate with Flow Graph
Software Metric
Measurement provides a quantitative idea or indication of the size, dimension or capacity of a feature or an attribute of a software product or process. Software metric can be defined as a quantitative measure of an attribute of a software related to its cost, quality, size and schedule.
For example −
Measure − Number of Errors
Metrics − Number of Errors detected per person
Cyclomatic Complexity in Software Testing
In software testing, cyclomatic complexity of a code section is a metric, that is used to measure the complexity of a given software program. It can be defined as a quantitative measure of independent paths in the source code of the software product. It can be measured through control flow graphs or through functions, modules, methods or classes in a software program.
An independent path is that path which has at least one edge that has not been yet traversed in any other path. In 1976, Thomas J. McCabe developed the cyclomatic complexity. Cyclomatic complexity was based on the control flow representation of the software program. The control flow represents a software program in the form of a graph, that comprises of nodes and edges.
The nodes represent the processing tasks, and the edges represent the control flow between the nodes.
Flow Graph Notation for a Software Program
Flow graph notation defines the nodes that are connected through the edges. Following are the flow diagrams for if-else, while statements and normal sequence of flow.
How to calculate Cyclomatic Complexity
Mathematical representation − Cyclomatic complexity is a set of independent paths through the graph diagram. It is represented through the formula −
V(G) = E – N + 2, where E = number of edges, N = number of nodes
Or,
V(G) = P + 1, where P = number of predicate nodes, that contain condition
Properties of Cyclomatic Complexity
V(G) denotes the maximum number of independent paths represented through the graph.
V(G) > 1
If V(G) = 1, i.e., V(G) is unity, then G can have only one path.
Reduce the complexity to 10.
How is Cyclomatic Complexity useful for software testing?
Basis path testing is a technique used in White Box testing to ensure the execution of at least one statement during software testing. Cyclomatic complexity assesses all the linearly independent paths through the program, i.e., the number of test cases will be equal to the cyclomatic complexity of the software program. It is useful due to its properties of cyclomatic complexity (M) −
M is the number of test cases required to achieve branch coverage (called upper bound).
Or, M can be the number of paths, including the independent paths, through the graphs (called lower bound).
Procedure for Computing Cyclomatic Complexity and Designing Test Cases
Construct a graph with nodes and edges from the software code.
Identify the independent paths.
If V(G) = 1, i.e., V(G) is unity, then G can have only one path.
Design the test cases.
After the basic set has been formed, the test cases must be written to implement each path.
V(G) − Cyclomatic complexity can also be manually calculated provided that the software program is small. Automated tool, that are required to if the program is too complex, because it involves more flow graphs. On the basis of the complexity number, the project team decide upon the actions that need to be implemented for measure.
The table below provides an overview of the complexity number and corresponding meaning of V(G) −
Complexity | Meaning |
---|---|
1 – 10 | Structured, well written code
High testability
Less cost and effort |
10 – 20 | Complex code Medium testability Medium cost and effort |
20 – 40 | Too complex code
Low testability
High cost and effort |
> 40 | Cannot be tested
Very high cost and effort |
Tools for calculating Cyclomatic Complexity
There are many tools available in the global market, that are used to determine the complexity of the software product or application. Some tools are used for certain technologies. Complexity can be determined by the number of decision points in the software program. For example, if, for, for-each, while, do, catch, case statements in a source code.
Some of the tools for calculating cyclomatic complexity are −
OClint
This is a static code analysis tool used to improve the quality of the software product and also to reduce the defects by investigating the C, C++, Objective-C code, and by finding out possible problems like −
Possible bugs − if, else, try, catch, finally statements
Unused code − This includes local variables and parameters that are unused.
Complicated code − This includes very high cyclomatic complexity, NPath complexity and very high NCSS.
Redundant code − This includes redundant if statements, useless parentheses, etc.
Code smells − This includes long methods and long parameter lists.
Bad practices − This includes inverted logic and parameter assignment.
With OClint, we can automate the inspection process with advanced features −
Dependency on the abstract syntax tree of the source code of the software for better accuracy and efficacy. The false positives are reduced to prevent useful results from sinking in them.
It enables the dynamic loading of rules into the system, even during the runtime.
It offers agile and extensible configurations to make sure that the users in customizing the behaviours of the tool.
Its command line invocation enables Continuous Integration (CI) and continuous inspection of the software code, so that the technical debts can be settled as early as possible to reduce the cost spent on maintenance.
GTMetrix
This tool is used to analyse the loading speed of pages and their performance. It provides scores for our pages and Pagespeed along with WiSlow. It is an open-source tool, used to get detailed reports about the site performance. It also provides recommendations for improving the scores of the web pages. It also helps us run analytics from the WordPress.
It also helps us schedule daily, weekly, or monthly reports. It also notifies us about the status of the website from a WordPress admin. First, we need to create a GTMetrix account. The account is free for a limited time and to some number of requests per day.
Reflector Add In
This tool is used for code metric for .NET assemblies.
Use of Cyclomatic Complexity
It also guides the testing process, and is easy to apply.
It also helps developers assure their clients that all the paths have been tested thoroughly.
It helps emphasize more on the uncovered paths.
It helps emphasize more on the uncovered paths.
Cyclomatic complexity metric helps increase the code coverage.
It helps estimate the risk in the application or software program.
Metrics help reduce more risk of the program, if used early.
Advantages of Cyclomatic Complexity
Cyclomatic complexity can be used as a quality metric.
It provides relative complexity of various designs.
It can computer fast than the Halstead’s metrics.
Cyclomatic complexity is also used to measure the minimum effort and the best areas of concentration.
It also guides the testing process, and is easy to apply.
Disadvantages of Cyclomatic Complexity
Cyclomatic complexity is the measure of the control complexity of the software program instead of the data complexity.
The nested conditional structures are much harder and complex to analyse than he non-tested structures.
For simple comparisons and decision structures, cyclomatic complexity may give a misleading figure.
Conclusion
Cyclomatic complexity is a software metric technique. It is used for structured or White Box testing. It is primarily used for evaluating complexity of the software program. The complexity of the software program increases, if it has more decision points. For software programs with high complexity, the probability of the error increases with time, for maintenance and trouble shoot.
- Related Articles
- Asymptotic Complexity
- Amortized Complexity
- C++ Program to Implement Quick Sort with Given Complexity Constraint
- What is 'Space Complexity’?
- Complexity of Interval Heap Operations
- C++ Program to Find Second Smallest of n Elements with Given Complexity Constraint
- Amortized time complexity in Data Structures
- C++ program to Calculate the Edge Cover of a Graph
- An interesting time complexity question in C++
- Time and Space Complexity in Data Structure
- Control the flow and formatting of text with CSS
- An Insertion Sort time complexity question in C++
- Practice Questions on Time Complexity Analysis in C++
- What is the complexity of the Apriori Algorithm?
- Time and Space Complexity Analysis of Queue operations
