Cyclomatic Complexity is a software metric used to calculate the complexity of a program.
It is a measure of number of possible use case flows for a given program’s source code and is directly dependent upon the number of control flow statements (if/else statements and switch cases) within the code.
For example, a method with no control flow statements would have Cyclomatic Complexity value of 1.
Cyclomatic Value as calculated by Visual Studio for a method that has no control flow statements:
And if we add one control flow statement, the Cyclomatic Complexity value will increase by 1.
private static void Divide(int x, int y) { if (x > y) { } else { } }
The code above has Cyclomatic Complexity of 2.
Basically, the Cyclomatic Complexity tells us how much complex our code is.
Since the more complex a code is, the more difficult it becomes to write a Test Case and validate it and so the higher value for Cyclomatic Complexity is usually associated with higher error prone code.
Hence, it is generally considered a good practice for software developers to write programs with low Cyclomatic Complexity.
Comments
Comments are closed.