6

My question is more about the transformation from programming code to control flow graph.

Say, I have a piece of code:

public class Contractor
{
    // other member fields... 
    private bool isVerified;
    private int noOfA;
    private int noOfB;

    // other member methods... 
    public int GetNumberOfDependents()
    {
        this.noOfB = this.noOfA;

        if (this.isVerified)
        {
            this.noOfB++;
        }

        if (this.noOfB > 4)
        {
            this.noOfB = 4;
        }

        return this.noOfB;
    }
}

I drew a flow diagram as below:

enter image description here

And please note that I didn't draw a node for the condition expression of IF statement, because I dont think it is a 'command'.

According to the Wikipedia page about CC, the definition of node is:

the nodes of the graph correspond to indivisible groups of commands of a program

And the formula is:

M = E − N + 2P

So I got its CC value as 4.

However, according to the description in this link, I got its CC value as 3.

There is a discrepancy here.

Moreover, according to David Tonhofer's answer to the question “Understanding Cyclomatic Complexity” on Programmers.SE, the formula in should be:

v(G) = e - v + p

That answer is not acknowledged by anyone, my question is: is my diagram correct?

  • Don't bother. McCabe Cyclomatic Complexity (MCC) has been shown, on real code, to be VERY (emphasis added) strongly correlated with raw number of source lines of code (SLOC). This means that MCC has essentially no predictive utility over raw SLOC, and it is a lot easier to count SLOC. – John R. Strohm May 8 '16 at 7:47
  • if you follow the "why we dont use it" link on wikipedia "Unfortunately, the original paper is vague on some details of the metric, such as how to derive the control flow graph, and hence different implementations often result in different measured complexity values for the same code" – Ewan May 8 '16 at 8:42
2

Your flow diagram can be simplified as:

[this.noOfB = this.noOfA;]
   |           \
   |            \
   |           [noOfB++]
   |            /
   |           /
[-----------------]
   |           \
   |            \
   |           [noOfB = 4]
   |            /
   |           /
[-----------------]

This gives 5 nodes, 6 edges, and 1 connected component => M = 6 - 5 + 2*1 = 3. Generally speaking, cyclomatic complexity is usually calculated using control flow graphs that only have at most two edges leaving each node.

| improve this answer | |
  • If you add a node for one if statement's condition expression, then you should add another one node for the other if, am I right? – VincentZHANG May 9 '16 at 4:24
  • There should be a node for every "basic block" in the function, that is any group of statements that terminates with a (potential) branch. The first node includes initialisation up to the first if statement's condition, therefore. You could theoretically split such a node (it would add one node and one edge, which cancel out in the equation), but there is little benefit to doing so. – Jules May 9 '16 at 18:51
-1

The simplest solution to this question is to count the number of condition checks in the entire code and then add 1 to the result which will bring the cyclomatic complexity as output.

Solution Condition 1 :- if (this.isVerified) Condition 2 :- if (this.noOfB > 4)

So the total no of predicates or conditions is 2 and formula is V(G) = N(P) + 1 = 2 + 1 Hence answer is 3

| improve this answer | |
  • The OP asked if the diagram is correct. The link embedded in the question had a good explanation of how to analyze a diagram to arrive at v(G). – Jay Elston Nov 13 '17 at 16:40
-2

One thing that you're missing here that substantially increases the complexity of the function is that it's a member function that is mutating the state of the object. You seem to have modelled it as if they were just regular local variables.

| improve this answer | |
  • 2
    While this may affect actual useful analysis of the function's complexity, cyclomatic complexity does not take into account such differences, but only the number of branching points. – Jules May 9 '16 at 0:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.