What is cyclomatic complexity?

The term that I see from time to time is "Cyclomatic Complexity". Here on SO, I saw some questions about “how to calculate the CC of a language X” or “How to make Y with a minimal amount of CC,” but I'm not sure I really understand what it is.

On the NDepend Website, I saw an explanation that basically says: “The number of solutions in the method. Everyone, if, for, &&, etc. adds +1 to CC“ score ”). Is this true? If so, why is it bad? I see that you might want to keep the number of if statements fairly low in order to simplify the code to understand, but is it really for this?

Or is there a deeper concept?

+68
language-agnostic architecture cyclomatic-complexity
May 26 '09 at 16:43
source share
15 answers

I do not know a deeper concept. I think this is usually seen in the context of maintainability index. The more branches exist within a certain method, the more difficult it is to maintain a mental model of the operation of this method (in general).

Methods with higher cyclic complexity are also more difficult to get full code coverage in unit tests. (Thanks Mark W !)

This, of course, introduces all other aspects of maintainability. The probability of errors / regressions / etc. The basic concept is quite simple, however.

+52
May 26 '09 at 16:46
source share

Cyclomatic complexity measures the number of times you must execute a block of code with various parameters in order to execute each path through this block. A higher score is bad because it increases the chances that logical errors speed up your testing strategy.

+36
May 26 '09 at 16:49
source share
Cyclocmatic complexity = Number of decision points + 1 

Decision points can be your conditional statements, such as if, if ... else, switch, for loop, while loop, etc.

The following table describes the type of application.

  • Cyclomatic complexity lies 1 - 10  Normal applicatinon is considered

  • Cyclomatic Complexity lies 11 - 20  Moderate use

  • Cyclomatic Complexity lies 21 - 50  Risky application

  • Cyclomatic complexity is more than 50  Unstable application

+12
Jul 03 '13 at 9:27
source share

Wikipedia may be your friend on this: defining cyclomatic complexity

Essentially, you should present your program as a control flow graph, and then

Complexity (...) is defined as:

 M = E − N + 2P 

Where

  • M = cyclomatic complexity
  • E = number of edges of the graph
  • N = number of graph nodes
  • P = number of connected components

CC is a concept that tries to understand how complex your program is and how difficult it is to test it with a single integer.

+11
May 26 '09 at 16:55
source share

Yes this is true. The more ways your code can execute, the more things you need to test, and the greater the likelihood of an error.

+7
May 26 '09 at 16:47
source share

Another interesting point that I heard:

The spaces in your code with the largest indentation should have the highest CC. These are generally the most important areas for ensuring test coverage, as they are expected to be more difficult to read / maintain. As other answers point out, these are also more complex code regions to provide coverage.

+4
May 26 '09 at 17:05
source share

Cyclomatic Complexity is really just a scary buzzword. This is actually a measure of code complexity used in software development to indicate more complex parts of the code (this is most likely a mistake, and therefore needs to be thoroughly and thoroughly tested). You can calculate it using the formula EN + 2P, but I would suggest that you calculate it automatically by the plugin. I heard about the rule of thumb that you should strive to keep CC below 5 to ensure good readability and maintainability of your code.

I recently experimented with the Eclipse Metrics Plugin in my Java projects, and it has a very nice and concise help file that will of course integrate with your regular Eclipse reference and you can read some more definitions of various complexity measures and tips and tricks for improvement your code.

+3
May 26 '09 at 16:55
source share

This is what the idea is that a low CC method has fewer forks, loops, etc., which all make the method more complicated. Imagine that you look at 500,000 lines of code using an analyzer and you will see several methods that have a higher CC. This allows you to then focus on reorganizing these methods for a better understanding (it also often happens that a high CC has a high error rate)

+2
May 26 '09 at 16:48
source share

Each decision point in a routine (loop, switch, if, etc.) essentially boils down to the if equivalent. For each if you have 2 encodings that you can take. So, with the 1st branch there are 2 code paths, with the second - 4 possible paths, with the 3rd - 8 and so on. There are at least 2 ** N code paths, where N is the number of branches.

This makes it difficult to understand code behavior and test it when N grows for a small number.

+2
May 26 '09 at 16:51
source share

The answers provided do not yet mention the correlation of software quality with cyclic complexity. Studies have shown that using a lower cyclometric complexity metric should help in developing software with higher quality. It can help with attributes of software quality, readability, maintainability and portability. In general, you should try to get a metric of cyclic complexity between 5-10.

One of the reasons for using indicators such as cyclic complexity is that, in general, a person can only track about 7 (plus or minus 2) information at a time in your brain. Therefore, if your software is too complex with several solution paths, it is unlikely that you will be able to visualize how your software will work (i.e., it will have a high metric complexity metric). This is likely to lead to the development of erroneous or patched software. More information about this can be found here , as well as on Wikipedia .

+2
Feb 10 2018-12-12T00:
source share

Cyclomatic complexity is calculated using a control flow graph. The number of quantitative measures of linearly independent paths through software source code is called Cyclomatic Complexity (if / if else / for / while)

+2
Jun 03 '17 at 22:58
source share

Consider a control flow chart of your function with an additional edge going from exit to entrance. Cyclomatic complexity is the maximum number of cuts that we can make without dividing the graph into two parts.

For example:

 function F: if condition1: ... else: ... if condition2: ... else: ... 

Control flow graph

Control flow chart

Perhaps you can intuitively understand why the connected graph has cyclomatic complexity 3.

+1
May 26 '09 at 16:52
source share

Cyclomatric complex is basically a metric for identifying areas of code that need greater accessibility for maintainability. This will be the main contribution to refactoring. It definitely gives an idea of ​​the area of ​​code improvement in terms of preventing a deep nested loop, conditions, etc.

+1
May 26, '09 at 16:53
source share

This kind. However, each branch of a case or switch statement tends to count 1. Actually, this means that CC hates case statements and any code that requires them (command processors, state machines, etc.).

+1
May 26 '09 at 17:29
source share

Cyclomatic complexity is a measure of how complex a piece of software is. It measures the number of different paths that a program can follow with conditional logical constructs (if, while, for, switch & case, etc. ....). If you want to know more about its calculation, here is an excellent YouTube video that you can watch https://www.youtube.com/watch?v=PlCGomvu-NM

This is important when developing test cases, as it reveals various paths or scenarios that the program can use. “In order to have good testability and maintainability, McCabe recommends that no software module exceed the cyclomatic complexity of 10” (Marsic, 2012, p. 232).

Ref: Marsic., I. (2012, September). Software Engineering Rutgers University. Retrieved from www.ece.rutgers.edu/~marsic/books/SE/book-SE_marsic.pdf

0
May 22 '19 at
source share



All Articles