0
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.

2
  • 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.) May 20, 2017 at 3:39
  • 1
    I 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
    May 20, 2017 at 5:18

1 Answer 1

2

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 an or so there are 3 ways that you can go through that leg, (either or both condition being true).
  • The second if contains an and so there are 3 ways, (either or both condition being false), to go through the else

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.

2
  • So, as per you what should be its McCabe value? May 20, 2017 at 19:35
  • 1
    This 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
    May 20, 2017 at 20:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.