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 whiles should not count at all.

Question: How to compute cyclomatic complexity for empty whiles and if + breaks?

有帮助吗?

解决方案

I would first say that you should generally favor not using infinite loops with an internal break. They are much harder to follow for readers of your code and likely to cause maintenance problems.

That said, a while(true) is not an "empty while." From the point of view of cyclomatic complexity we want to know how often the code branches. There's still a branch at the end of the loop that goes back to the beginning. You've added yet another branch with the if statement. So I would say that you should count it as additional complexity. In fact, I'm tempted to count your if...break as 2 since there's the branch for the if and the additional branch for the break.

Even if you don't agree with my assessment of the cyclomatic complexity, surely you can see that reading the code is harder with the if...break. In the original code, I can clearly see that we're decrementing x until it's equal to level. In the second code snippet, I have to think a lot harder to see that.

Furthermore break is confusing in C-based languages. It's most common use is in switch statements, where it's fairly easy to understand, since that's where most people first learn about it. Once you put it inside nested control structures, it becomes much more confusing. Does it break out of only the one it's in, like with a switch statement? Well that doesn't make sense here because it's the last line of an if. You could just not put it there and you'd be out of the if at the closing brace. So it must break out of the control structure outside of that. But what if that's inside another control structure? Does it break out of that too? If not, why not? And why when it's used with a switch does it not also break out of the enclosing control structure? As such, I tend to avoid using break anywhere other than switch statements.

许可以下: CC-BY-SA归因
scroll top