Thanks to a question over at Code Review I got into a little disagreement (which essentially is an opportunity to learn something) about what exactly the Cyclomatic Complexity is for the below code.
public static void main(String[] args) {
try {
thro();
thro();
thro();
thro();
thro();
thro();
thro();
}
catch (NullPointerException e) {
}
}
private static Random random = new Random();
public static void thro() throws NullPointerException {
if (random.nextBoolean())
throw new NullPointerException();
System.out.println("No crash this time");
}
When writing this code in Eclipse and using the Eclipse metrics plugin, it tells me that the McCabe Cyclomatic Complexity for the main method is 2, and for the thro
method it says 2.
However, someone else tells me that the complexity of calling thro
multiple times is number of calls * method complexity
, and therefore claims that the complexity of the main method is 7 * 2 = 14.
Are we measuring different things? Can both of us be correct? Or what is the actual cyclomatic complexity here?