Reputation: 1
Hi iam just learning the Cyclomatic Complexity and i dont get it. I have read some wikipediaposts and some random posts on the internet and there are always different solutions i think.
First of all What do I count. On many pages it says that i should begin with an 1 for the function and then add for each if i find is this correct?
http://docs.klocwork.com/Insight-10.0/McCabe_Cyclomatic_Complexity So in this equals the CCN should be 5 is this correct or did i miss something? I just dont get it what to count and what not since there are so manny different posts on the internet. Sry if this question is asked bevore but i was not able to find it.
So have a nice Sunday and enjoy ur time Is there like a simple "trick" how to see it very fast.. just wondering
http://s16.postimg.org/ufouegwut/number.png) This is the link to the snipped i hope it is not a problem that i not just wrote the code here
Upvotes: 0
Views: 161
Reputation: 5685
The cyclomatic complexity of a function is the number of paths the code can take through the function.
The example you point to:
Function
While
If
Else
Endif
Endwhile
End Function
Has the cyclomatic complexity of 3, this is because there's a path of not hitting the while, a path hitting the while, and a path hitting the if inside of the while (the else is part of the previous path that hit the while but not the if).
I find the simplest way is to count the number of possible branches: while
, for
, if
, switch
cases (not including the default
case), catch
, and ternary operators, then add 1.
Here are the branches that GMetrics counts:
Edit: Just noticed your example was actually at the end of your post, which as you correctly say has a cyclometric complexity of 5 as you have 4 branches (4 if statements), so 4 + 1 = 5
Upvotes: 1