if field is blank or in form
print "enter again"
else
if no credit and debit < 1000
print "less amount"
else
print "accessed"
end if
end if
I'm very confused in calculating the cyclomatic complexity. For the above program, I get the answer as 3, as there are two conditions and V(G)=P+1=2+1=3. But the answer is incorrect.
-
Don't bother. MCC (McCabe Cyclic Complexity) has been shown to be VERY strongly correlated with raw SLOC, when run against real code from one of the BIG repositories. (We're talking MILLIONS of SLOC here.)John R. Strohm– John R. Strohm2017年05月20日 03:39:35 +00:00Commented May 20, 2017 at 3:39
-
1I also get 3 for the code shown. But why is that supposed to be wrong? Please explain. Also, the flowchart seems to be totally unrelated with the code, why are you showing it?amon– amon2017年05月20日 05:18:08 +00:00Commented May 20, 2017 at 5:18
1 Answer 1
Calculating McCabe is not as simple as counting if
statements and adding one. You have to count the number of possible paths through your code.
In your example:
- The first
if
contains anor
so there are 3 ways that you can go through that leg, (either or both condition being true). - The second
if
contains anand
so there are 3 ways, (either or both condition being false), to go through theelse
The various tools that can be used to compute McCabe often disagree with each other and the tools that give a McCabe for the whole file are basically useless as they will give an exaggerated value if the file contains a lot of code. McCabe should only be considered on a per function basis but you can calculate the Maximum and Average values to highlight files that are the most in need of attention.
-
So, as per you what should be its McCabe value?user7943804– user79438042017年05月20日 19:35:44 +00:00Commented May 20, 2017 at 19:35
-
1This is an unusual interpretation of cyclomatic complexity. Normally you would only look at the control flow graph itself, not count a route through it twice if there are two different conditions that can cause a path to be taken. Admittedly things can get complicated when you account for the control flow through short circuited Boolean expressions, but the quoted code looks like visual basic which AFAIK does not short circuit Boolean operators.Jules– Jules2017年05月20日 20:37:00 +00:00Commented May 20, 2017 at 20:37