Search code examples
cyclomatic-complexity

Cyclomatic complexity of McCabe vs Yourself Drawing


So pretty much I was taught to work out the cyclomatic complexity like this site does But then recently I found this thing that says Cyclomatic Complexity = ( 1 + ifs + loops + cases ). Are they the same? is the thing I read perfect for every case? As I'm wondering whether it would miss out on something or doesn't take something into account when calculating? From what I understand it seems fine but it just feels a bit simple compared to drawing out everything.

Also if I have a loop such as

while (a==b && c>d) {
}

would I say there is 1 loop there (comp = 1 loop+1) or would I say there is 3 (1 loop +1 test+1 test+1) due to the while a==b and the c>d test parts. and how does that compare to say just

while(a>b) {
}

as I would assume that would have just 1 loop so (comp = 1 loop +1) I would also assume it would be the exact same as a FOR loop in terms of complexity.

as

for(int i =0; i<12; i++){
}

it has just the single loop.

Lastly if I have something like code, while loop with no if statements or anything then end code.

If I drew the paths do I just have one path doing code>loop>endcode. Or do I have two paths being:

1) code>loop>endcode

2) code>endcode Imagine that loop is the FOR loop I wrote above for example.

Apologies for all the questions, it seems to be loops in particular I'm super confused with.


Solution

  • As far as I know, the flow path analysis is not the same as using keywords, but is close. I even find the original paper at http://www.literateprogramming.com/mccabe.pdf extremely confusing.

    The word paths is used in two ways. One is as an edge in the graph, and the other is as a set of edges, also called an independent circuit. I will use the terms edge (single edge) and path (independent circuit).

    The appendix contains the technique using keywords. The flow graph for the example SEARCH function looks like there are 12 edges - 11 nodes + 2 = 3 paths, but the text says there are four. The code shows four, which matches the text, but there are only three paths if only statements are counted that modify something.

    I think one important point that is not defined in the original paper is whether a group of statements modifies something, or is used later to modify something. This is particularly relevant to your question about compound conditionals.

    The question about compound conditionals (Meyers) is discussed here http://www.researchgate.net/publication/3407068_A_Critique_of_Cyclomatic_Complexity_as_a_Software_Metric in the section named "Theoretical considerations". Compound conditionals are even more tricky and important in a language like C or C++, since they have short-circuit evaluation.
    http://en.wikipedia.org/wiki/Short-circuit_evaluation.

    There can be a conditional like:

    while(a==b && dofunction() {
    }
    

    The dofunction() will only execute if a is not equal to b.

    For your last question about the for loop, I am assuming there is something like:

    statements (initial)
    for(int i =0; i<12; i++)
        {
        statements (body)
        }
    statements (end)
    

    Sorry, it would be nice if I could draw a graph, but I do not have enough points.
    I would place nodes like:

    node(A)
        |
        | statements (initial)
        |
    node(B)   (for loop)
        |                 |
        | no statements   |  statements (body)
        |                 |
    node(C)   (meet point at end of for loop)
        |
        | statements (end)
        |
    node(D)
    

    A-B has one edge B-C has two edges - one for the condition taken, and one for skipped. C-D has one edge

    There are only two paths through the code. The complexity is calculated as: 4 nodes - 4 edges + 2 = cyclomatic complexity of 2.

    There is a document describing this here, and even more examples are systematically analyzed. http://oovaide.sourceforge.net/articles/Complexity.html. This document also shows that some tools count logical operators and some don't.