Domanda

I am studying Cyclomatic Complexity in my Software Quality Assurance course at the University and I am having a hard time understanding how it works when you have compound conditions in a predicate statement or node. I have seen multiple definitions of Cyclomatic Complexity, the primary of which used my in my class is

V(G) = # of predicate nodes (with outdegree = 2) + 1

So for a program graph such as this we would have a cyclomatic complexity of two:

V(G) = 2

Program graph with cyclomatic complexity of two

I have also seen definitions of the Cyclomatic complexity given as

V(G) = # edges - # nodes + 2

Which also works for the graph given above. However, we we have compound conditions for predicate nodes. And we consider a language and system set up for automatic short circuiting, it seems to me the graph on the below to the left would have to be expanded to the graph. If this is the case such as below, does the Cyclomatic complexity become 3, even if in the real source code, we may only have a single if statement or is it still V(G) = 2?.

It is confusing because most of the definitions I see of Cyclomatic complexity are talking about predicate nodes, and as I understand predicates they may contain multiple short-circuited conditions. If this not the case, it seems that short-circuiting behavior, while giving performance increases, actually raises Cyclomatic complexity when considered. If source code is given, must all the conditions be broken into their own nodes such as the graph on the left before we can calculate cyclomatic complexity? Program graph expanded to show short circuiting behavior

È stato utile?

Soluzione

In C (C++, Java, C#) the "&&" operator adds one to complexity because it's defined to use short-circuit evaluation. Cyclomatic Complexity in this case would be 3.

See http://hissa.nist.gov/HHRFdata/Artifacts/ITLdoc/235/chapter4.htm

"Boolean operators add either one or nothing to complexity, depending on whether they have short-circuit evaluation semantics that can lead to conditional execution of side-effects."

Figure 4-4 has annotated source code and a corresponding flow graph that demonstrate how complexity is determined.

See also Cyclomatic complexity of IF((A>B) AND (C>D)) and IF((A>B) OR (C>D))

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top