we are just asking ourselve why Jacoco uses the following formula to calculate the cyclomatic complexity:
v(G) = B - D + 1 (B = number of branches, D = number of decision points)
as far as we did research into cyclomatic complexity there are 3 methods to calculate the cyclomatic complexity:
Method 1: CC = E - N + 2 (N = Nodes / E = Edges)
Method 2: CC = Total number of bounded areas + 1 (bounded area = any region enclosed by nodes and edges)
Method 3: CC = Total number of Decision Points + 1
I am asking this question because we have cases where the total number of branches in a class is higher than the cyclomatic complexity. Is this the right behaviour? Why do you subtract the decision points from the number of branches?
On the other hand to achieve 100% branch coverage you would just need 2 test cases. For example:
1.) cond1 = true, cond2 = true
2.) cond1 = false, cond2 = false
See also example from [4].
In addition 100% path coverage implies 100% branch coverage [4] but not vice versa.
So I conclude that the number of (linearly independent) paths through a method needed for 100% path coverage should always be greater or equals than the number of branches to achieve 100% branch coverage. That's why the cyclomatic complexity number should always be greater or equals than the number of branches, right?
I hope I used the correct naming and I'm not missing something. Please correct me if I'm wrong.
Best regards
Markus
Sources:
[1] https://en.wikipedia.org/wiki/Cyclomatic_complexity
[2] http://www.onjava.com/pub/a/onjava/2007/03/02/statement-branch-and-path-coverage-testing-in-java.html?page=2
[3] http://users.csc.calpoly.edu/~jdalbey/206/Lectures/BasisPathTutorial/
[4] https://sites.google.com/site/swtestingconcepts/home/test-design-techniques/for-white-box/statement-branch-and-path-coverage