Do you find cyclomatic complexity a useful measure?

I played with measuring the cyclic complexity of a large code base.

Cyclomatic complexity is the number of linearly independent paths through the program source code, and there are many free tools for your choice of language.

The results are interesting, but not surprising. That is, the parts that, as I know, were the most hairy, were actually the most difficult (with a rating> 50). But what I find useful is that a specific “badness” number is assigned to each method as something I can indicate when it decides where to start refactoring.

Do you use cyclomatic complexity? What is the most difficult bit of code you found?

+52
metrics refactoring code-metrics cyclomatic-complexity
Apr 14 '09 at 0:02
source share
15 answers

We mercilessly reorganize and use Cyclomatic complexity as one of the indicators that gets the code on our hit list. 1-6 we do not note complexity (although this can be called into question for other reasons), 7-9 is doubtful, and any method over 10 is considered bad, unless proved otherwise.

The worst thing we saw was 87 from the monstrous if-else-if chain in some legacy code that we had to take over.

+36
Apr 14 '09 at 0:13
source share

In fact, cyclic complexity can be used for use outside of the threshold level of the method. For starters, one large method with high complexity can be broken down into several small methods with less complexity. But did it really improve the codebase? Of course, you can get better readability with all these method names. But the general conditional logic has not changed. And general conditional logic can often be reduced by replacing conventions with polymorphism .

We need a metric that does not turn green, by simply decomposing the method. I call it CC100 .

CC100 = 100 * (Total cyclic complexity of the code base) / (Total lines of code)

+15
Jun 28 '09 at 7:00
source share

This is useful to me just as big-O is useful: I know what it is and can use it to get a gut feeling about whether a good or bad method, but I don’t need to calculate this for every function I wrote .

I think that in most cases simpler metrics like LOC are at least good. If a function does not fit on a single screen, it doesn't really matter how simple it is. If a function takes 20 parameters and is 40 local variables, it does not matter if its cyclic complexity is equal.

+11
Apr 14 '09 at 1:00 a.m.
source share

We recently started using it. We use NDepend to analyze static code, and it measures cyclic complexity. I agree, this is a decent way to define refactoring methods.

Unfortunately, we saw # above 200 for some methods created by our developers in offshore.

+7
Apr 14 '09 at 0:06
source share

As long as there is a tool that can work well with C ++ templates and metaprogramming methods, this does not help much in my situation. Anyway, just remember that

"not all things that are considered may not be all that can be measured by a score" Einstein

Therefore, be sure to convey any information of this type using people filtering.

+7
Apr 14 '09 at 0:42
source share

You will see complexity when you see it. The main thing that this tool is useful is to mark out parts of the code that were of interest to you.

+6
Apr 14 '09 at 1:05
source share

I often measure the cyclomatic complexity of my code. I found this to help me identify areas of code that do too much. The presence of the tool indicates that the hot spots in my code are much shorter than reading thousands of lines of code trying to figure out which methods do not follow SRP.

However, I found that when I do cyclic complexity analysis for other people's code, it usually leads to feelings of frustration, longing and general anger when I find code with cyclic complexity in the 100s. What makes people write methods that have several thousand lines of code ?!

+4
Apr 14 '09 at 1:02
source share

This is great for finding candidates for refactoring, but it’s important to keep your opinion. I would support kenj0418 ranges for trim guides.

+3
Apr 14 '09 at 1:12
source share

There is a Java metric called CRAP4J , which empirically combines cyclic complexity and JUnit coverage to cover a single metric. He was engaged in research to try to improve his empirical formula. I'm not sure how common this is.

+2
Apr 14 '09 at 0:52
source share

I have not used it for a while, but in a previous project it really helped to identify potential problems in something elses code (of course, would not be!)

Finding an area to check, I quickly found many problems (also a lot of GOTOS, you would believe!) With logic and some really weird WTF code.

Cyclomatic complexity is useful for showing areas that are likely to do a lot, and therefore break the only responsible privilege. These ideals should be broken down into mulitple functions

+1
Apr 14 '09 at 0:07
source share

I’m afraid that for the language of the project for which I like these indicators the most, LPC , in fact, there aren’t many free tools for creating it. So no, not so useful to me.

+1
Apr 14 '09 at 0:15
source share

+1 for kenj0418 hit list values.

The worst that I saw was 275. There were several more for 200 that we were able to reorganize to a much smaller Central Committee; they were still tall, but they made them retreat back in line. We were not very lucky with 275 animals - it was (probably still there) a network of if- and switch statements, which was just too complicated. This is real value - it is step by step when they decide to rebuild the system.

The exceptions for the high CC, which I was comfortable with, were factories; IMO, they should have a high CC, but only if they perform a simple creation and return of an object.

+1
May 26 '09 at 18:19
source share

After understanding what this means, I now began to use it on a “trial basis”. So far, I have found this to be useful, because usually a high CC goes hand in hand with an Arrow Anti-Pattern , which makes the code harder to read and understand. I don’t have a fixed number yet, but NDepend warns about everything above 5, which seems like a good start to learning the methods.

+1
Jun 09 '09 at 9:39
source share

Yes, we use it, and I found it useful. We have a large outdated code base for taming, and we have found a complex complex complexity. (387 in one method!). CC points you directly to areas worth refactoring. We use UDP in C ++ code.

+1
Sep 09 '09 at 10:51
source share

Cyclomatic Complexity is just one of the composers of what could be called Fabricated Complexity. Some time ago, I wrote an article to summarize several aspects of code complexity: Combating Complex Complexity

A tool is needed to effectively manage code complexity. The NDepend tool for .NET code allows you to analyze many aspects of code complexity, including code metrics such as: Cyclomatic complexity, nesting depth, lack of chaining methods, test coverage ...

including dependency analysis and including a language ( Code request language ) dedicated to a task that is difficult in my code and a rule for writing

+1
Aug 28 '10 at 8:56
source share



All Articles