Thursday, December 29, 2016

Cyclomatic Complexity and C Code

Cyclomatic Complexity refers to the code complexity and the relevent threat level of code having bugs. It's measured as counts and the range is generally 1-10 good - very less possible bugs, 10-20 average - few bugs possible, 20-30 High threat - high number of bugs possible due to high complexity, >30 is very dangerous with very high number of complex bugs possible.

Generally a program counting the Cyclomatic complexity goes through the code and
counts the various conditions, branches, go to's etc. Then by counting the edges of code flow graph w r t nodes, the CC count is calculated.

What I have to do with all this? Basically in my current organization we have CBA - continuous build automation facility which produces nightly builds of all projects. It also performs linting, static code analysis and produces various red/yellow etc flags for code quality issues. Recently a red flag was seen in a feature I halped integrate.
This is a third party component, but since we integrate it in source form, the CBA issues become our responsibility.

Going through the code I found the flagged c - functions to be complex code with about 25+ if else conditions and couple of switch cases each.

So now to make this code CBA clear, I'm going to have to refactor it into cleaner code.

To do this I am using "cccc" tool. Following link proved very handy
http://baptiste-wicht.com/posts/2011/08/compute-metrics-of-c-project-using-cccc.html
cccc lets me check in detail what's wrong with cyclomatic complexity of functions and where. Then when we have cleaned them up we can check the result with cccc again to validate the changes.

Similar too is an eclipse plugin  by name of metriculator. So if you are using eclipse as IDE for your C/C++/Java code then you can use this.

No comments:

Post a Comment