15
Regarding the complexity of algorithms, I observed that there are several citations about cyclomatic complexity. What is cyclomatic complexity? In which situation is it important to analyze this complexity?
15
Regarding the complexity of algorithms, I observed that there are several citations about cyclomatic complexity. What is cyclomatic complexity? In which situation is it important to analyze this complexity?
17
It is a measure of the complexity of an algorithm where the independent paths that the algorithm can take are considered. The greater the cyclomatic complexity, the harder it is to track the code, maintain, test and fully cover.
The cyclomatic complexity increases whenever there is a branch, but some forms of branch can generate paths that are harder to track than others. The more linear, the easier it is to keep track of the code.
A function output is still a branch and counts to increase cyclomatic complexity, so there is no CC function 0.
So use several return
or possibly the use of goto
may increase or decrease cyclomatic complexity depending on how they are used, even if it may harm another aspect.
Large functions tend to have greater cyclomatic complexity. It is very rare to be able to do something great without branches.
The analysis passes to represent the execution flow through a graph. An example taken from Wikipedia:
This code has CC 3 since there are 2 branches typical (a loop and a conditional) and 1 exit point.
The average developer just needs to know to keep the complexity low by simplifying the possible paths. This makes your work much easier and decreases the amount of problems that can occur. More mathematical questions are useful for computer scientists or engineers who need to work with more formal development or very optimized routines.
One point to consider is that breaking a function into several can decrease the CC of this function, but not decrease the CC of the algorithm as a whole. It might even increase. Some people might say that at least it gets easier to test, and this is even true for the individualized test that has little value, you still need to test all situations and this has to consider the results produced by the auxiliary functions.
Problems as a whole have a limit of what you can reduce CC. What the developer can do is decrease where there is CC in exaggeration, the unnecessary.
In which situation is it important to analyze this complexity?
In any situation that is not a trivial and perhaps disposable code it would be good to think about it and see if it can simplify something.
There are tools to measure the cyclomatic complexity of your code in several languages, some even suggest a refactoring that can simplify. So you don’t have to waste time doing it manually.
An example that even concretely shows how she does the math.
I reinforce that it alone is not a good indication of code quality. You can decrease the CC and the code get worse. You can opt for polymorphism and pass having intrinsic decisions and the execution pass have more differences of execution.
Something caught my attention studying the subject and it was not so clear whether the complexity of the algorithm is about the number of possible paths or the number of decisions to be considered. I saw divergent materials about this, when I was ambiguous about using terms that nobody knows exactly what that is, the formula is good but you don’t know what each element of the formula is.
If the CC considers only the different code blocks according to what it sees written then I find a less important measure.
If you consider the number of different decisions to decide whether to take a path seems more important to me.
I don’t even know if this is officially correct, but if I’m wrong I’d rather keep the "wrong" because it makes more sense, it’s more logical, it’s that people might think, "if you have a if
then there’s a branch and the CC rises 1". It could not be well.
Under normal conditions at least 1 rises even, but if the if
for true
or false
already determined by the code without depending on the execution it should go up 0.
Using the same logic, if you have a more complex condition in if
or even in expression outside it, complexity can rise further, and it can even become exponential. Just the fact of using a comparison concatenation operator like the and
or or
already makes a simple if
have two branches, then a if
with 30 and
s makes the CC go close to 1 billion. Why does this happen? Because at the bottom each subcondition is a separate decision, it’s like a if
new, but everything is concatenated.
And then a loop could vary according to the amount of possible interactions. But that’s more like algorithm complexity.
This may not be CC, but it’s more important for your code. Including because it’s really weird that you have 10 if
s whose execution block does the same thing and if you put it all together in a single if
the complexity change.
More recently I’ve heard of cognitive complexity. I haven’t seen much more reliable material about it (saw this), But there’s software measuring this kind of thing and consider all that I said. It seems a more appropriate measure, although I’m not saying it’s the same thing.
It’s obvious that a measure that says complexity is 1 billion helps little if it can’t be reduced.
I’m not saying that cognitive complexity is better because I think it has its flaws, too. All are useful to give an indicator, but need to understand the limitation of each. Just be aware that there are several measures and they should be observed when coding and not doing everything in the style "My ox" is already an advance that every programmer should follow.
6
Illustrated cyclomatic complexity:
Think about how in this case would be the graph that @Maniero exemplified above. :-)
It was kind of interesting to put that, because there’s the myth that if you have several ifs
not nested, or if you put all this with &&
, because in the end it is one thing, it diminishes the cyclomatic complexity, but it does not diminish anything, the amount of branches is the same. It is only more readable. Even more readable, with a single if
(actually need two) the CC of this code would still be 13 (I did it in my head quickly, I may have missed). I mean, that’s curious, but it doesn’t really illustrate the CC.
I agree! Is that I did not find an image where beyond the ifs
there were also elseifs
, elses
and cases
(in fact, I think that whoever makes a drawing like this will be admitted later, these things affect the sanity), which would cause the WC to be changed. Anyway, it’s an image, as you said, curious, and fun for the topic. :-)
To complement, 13 (if not conferi) is already a strange number (at least IMHO) ... here are some examples of metrics: http://stackoverflow.com/questions/1364946/what-is-the-highest-cyclomatic-complexity-of-any-function-you-maintain-and-how
Like I said, I may have done something wrong, but because 13 would be weird?
Here is a very nice article where they define 10 as the good value (I still think less than that acceptable ...) and 15 as a "hopefully it’s worth it if you know what you’re doing": http://www.mccabe.com/pdf/mccabe-nist235r.pdf. Anyway, in terms of readability, maintenance and code efficiency I prefer smaller numbers.
I just don’t understand why you think it’s weird, a twisted code like 13 is pretty normal.
I don’t think 13 is weird for this bizarre code, 13 is weird for CC. But even without the metric Just hit your eye on the image that you can see that the thing there is ugly. ;-)
Browser other questions tagged terminology software-engineering testing flow-control
You are not signed in. Login or sign up in order to post.
Behold on wikipedia or in English wikipedia.
– Victor Stafusa
@Victorstafusa my grandmother said that wikipedia anyone can put anything. Thinking about it, I prefer to know from here of the self qualified users of our Sopt; It is almost obvious that most questions not only about terminology have there on wikipedia, why should there be here then? The tag is useless?
– viana
It’s not like that. It’s true that anyone can edit on wikipedia, but that doesn’t belittle it, yet the content there is usually (but not always) very good (just like it happens here). However, I don’t have time to give you a complete and detailed answer (whoever you have, go ahead and write one), but wikipedia already gives you a way and if you (or someone else) are in a hurry to know, you can take a look there. In addition, this also serves to define a point of reference, any response here that is lower than what is there should be improved.
– Victor Stafusa
@Victorstafusa this is fact, sometimes the answers accepted here are very inferior. But my intention is that it be registered also here in the SO, also for future references.
– viana