Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to measure C++ or Java file complexity?

I want to start measuring what Michael Feathers has referred to as the turbulence of code, namely churn vs. complexity.

To do this, I need to measure the complexity of a C++ or Java file. So I found a couple tools that measure cyclomatic complexity (CC). They each measure CC well at the function or method level. However, I need a metric at the file level, and they don't do so well there. One tool just returns the average of all method complexities in the file, and the other tool treats the whole file like it is one giant method, i.e., it counts all the decision points in the whole file.

So I did some research and found that McCabe defines CC only in terms of modules--and they define a module as a function--not as a file (see slides 20 and 30 of this presentation). And I think that makes sense.

So now I'm left with trying to figure out how to represent file complexity. My thought is that I should just use the maximum method CC for that file.

Any thoughts about that approach or any other suggestions?

Thanks!

Ken

like image 588
Kenneth Klein Avatar asked Oct 03 '13 01:10

Kenneth Klein


People also ask

How do you measure code complexity?

You compute it by using the control flow graph of the program. Cyclomatic complexity measures the number of nested conditions within the code, such as those created by for, if/else, switch, while, and until. The greater the number of conditions (as in example b from above), the greater the complexity.

What is Java code complexity?

Code complexity is a measure of how difficult it is to understand your code. Generally, large method and classes are code smells. There are different measures for code complexity, and various tools to measure them as well. Keep Refactoring code to reduce complexity.

How is cyclomatic complexity calculated in Java?

Calculate cyclomatic complexity in JavaAssign one point to account for the start of the method. Add one point for each conditional construct, such as an "if" condition. Add one point for each iterative structure. Add one point for each case or default block in a switch statement.


1 Answers

Few years ago I had the same question. I answered it in the following way and it worked and works for me perfectly:

The purpose to minimize complexity is to improve maintainability. Cyclomatic complexity is an indicator of logical complexity, and you are right - it is applied to the smallest 'unit', i.e. function. It is possible to derive 'summary' metrics, like total/max/min/etc but they rarely show something useful, when it is about cyclomatic complexity. I tried to use 'summary' metrics to compare 2 code bases, but concluded that only distribution graphs of cyclomatic complexity are really useful here.

So, what could be used to indicate something about maintainability level for bigger units/levels of abstractions, like files/components/subsystems? I found that the first metric is a size of a unit in lines of code. If you limit the size of a file, like 1000 lines, and limit cyclomatic complexity for each function in the file, you will have relatively "simple" file, because it is "small" and contains only "simple" functions. You may include or exclude comment/blank lines or count only statements or only executable lines...

However, I concluded that it does not really matter in this particular application. Just limit some 'size' metric and it will serve the purpose in most cases.. Later you may think about limiting the total number of lines of code per a component/subsystem. It will have the same effect - component is "simple", because it contains "small" number of "simple" files.

The post you referred to is very good. It can be extended to broader metric, which usually is named as 'maintainability index'. The index is very high if a function is complex, file is big and has got frequent changes, little coverage by tests, and so on (add here whatever you think defines maintainability). It is the best way, I know, to find hot-spots for re-factoring...

Disclaimer: I am looking after Metrix++ tool which executes the use case scenario, I explained above.

like image 119
Andrew Avatar answered Oct 09 '22 01:10

Andrew