checkstyle computes the cyclomatic complexity of this method to be 16, I believe it should be 2, as the cases should all be one node.
public static void switchTest(){
int i=0;
switch(i){
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
break;
}
}
Normand Rivard
2007-06-01
Logged In: YES
user_id=59697
Originator: NO
I disagree with this. The proof is that it would take 16 test cases to prove the code is working fine. Technically speaking, the code can (and probably is) compiled into a set of 15 decisions.
On the other hand, since i is initialized with 0 at the top, all the other cases should be considered dead code, but that is a different story.
deusi
2008-02-21
Logged In: YES
user_id=1272002
Originator: NO
I disagree on the disagree ;-)
To speak in terms of "test cases" - yes, there might be at last 16 different test cases. But cyclomatic complexity does not count test cases but test paths (please have a look for all the McCabe Cyclomatic Complexity documents on the web). The "cc" is based on graph theory and this example only has a "cc" of 2.
Just think of a flowchart where "case 0" to "case 14" all are in one line and all lead to one node at the end. The flowchart would look like one for an IF-ELSE-statement. Furthermore, if you leave the "break" the "cc" would be still 2. The flowchart would look like one for an IF-statement.
Roman Ivanov
2015-09-24
Roman Ivanov
2015-09-24