Similarly, there should be an approach through which we can calculate the complexity of code written by the development team that could be correlated to the number of coding errors.
As a result, Cyclomatic complexity term comes into picture which is the measurement of a source code complexity. It is calculated through a control flow graph which is developed on the basis of source code which measures the number of linearly-independent paths through a program module. In this article, we will understand the concept and the formula to calculate Cyclomatic complexity with an example.
The target here is to have Cyclomatic complexity as low as possible for the best source code. If it is low then the risk to modify code is lower and the code will be easier to understand and maintain. Given below is the formula through which Cyclomatic complexity is evaluated.
Introduction to Cyclomatic Complexity
Here is the source code to display the values of different variables based on the comparison. I have taken 4 variables with different values.
CYCLOMATIC COMPLEXITY IN SOFTWARE ENGINEERING WITH TRICK
Below is the representation of control flow graph based on the demo source code we have written above. We will calculate the number of edges, nodes and the exit points to calculate Cyclomatic complexity using the formula.
After noticing the above control flow graph, we can evaluate the following values of Cyclomatic complexity parameters. Therefore, Cyclomatic complexity for the above control flow graph is 2 which is an acceptable number.
In a similar way, using the same formula we can calculate the Cyclomatic complexity for any source code and based on the result, we can determine the risks and associated maintenance cost. It can be really useful for overall project budgeting.
To understand the importance of Cyclomatic complexity from a QA perspective, the result we get from the formula determines how much testing is required to test that piece of source code.
Join Over 300,000+ Followers!
For example, if the number is higher, that piece of code requires in-depth testing compared to the code which has lower Cyclomatic complexity. Our testing scope and the methods would be defined according to the complexity of the functionality. The control flow graph for the code under test can be obtained from the developer to calculate the number of edges, nodes and exit points.
Also, a number of test cases for maximum test coverage can be determined as well based on the paths in the CFG control flow graph and the branches.
Steps to be followed:
In other words, Cyclomatic complexity is very helpful for QA to estimate the scope of testing, determine how skilled tester is required, and the extent of testing required for the best way possible code coverage.
Also, it is one of the best ways with which developers can make QA team to understand the source code complexity. Cyclomatic complexity calculation is one of the standard approaches to calculate the source code complexity and determine the risk that the source code possesses for any future modification and maintenance. Lower the Cyclomatic complexity better is the code quality in terms of complexity. If you are not a regular reader of this website then highly recommends you Sign up for our free email newsletter!!
Sign up just providing your email address below:. In the given diagram you have mentioned that Edges or Nodes are specified but i am not able to locate please clarify.
Make your .NET code beautiful with NDepend
I is not scrolling issue please send me the diagram to my email id sbalujob yahoo. S Balu 13 Aug S Balu 14 Aug STC Admin 19 Aug Please reload the page if the page is not scrollable. S Balu 19 Aug S Balu 20 Aug Close Menu.