This is closely related to this question which asks about the complexity of the following:
while (x > level)
x = x – 1;
x = 0
Using the graph method it has a Complexity = 2. Fred Swartz indicates a more faster way of computing complexity for Java/C# based on actual instructions from these languages:
- Start with a count of one for the method.
- Add one for each of the following flow-related elements that are found in the method.
Returns - Each return that isn't the last statement of a method.
Selection - if, else, case, default.
Loops - for, while, do-while, break, and continue.
Operators - &&, ||, ?, and :
Exceptions - catch, finally, throw, or throws clause.
However, nonconvergent
asks about how if .. break
should be treated. So, I am thinking rewriting above code like this:
while (true)
{
if (x > level)
break;
x = x – 1;
}
x = 0
While this is equivalent to the initial algorithm, I would theoretically add a complexity of 2 (if
+ break
).
My feeling is that if ... break
should count as 1 and empty while
s should not count at all.
Question: How to compute cyclomatic complexity for empty whiles and if + breaks?