-2

When defining McCabe's essential complexity, the idea of a structured programming control structure is present. I don't understand why an if-then-else, a while loop or a for loop can be reduced to a single statement such that it's cyclomatic complexity is of one.

Cedric Martens
  • 219
  • 1
  • 9
  • Can you cite the reference that says that? My reading of it suggests that loops add one rather than being one and the same one with the surrounding code. – Erik Eidt Oct 19 '20 at 01:27
  • It is possible that I have misunderstood the statement. I'm asking this question because I don't understand Essential Complexity. https://www.semanticscholar.org/paper/Structured-Testing%3A-A-Testing-Methodology-Using-the-Watson-McCabe/e2989a79a2ec634c17605c22906f9e2e15e283f6 In the fourth figure (10-2), right click and open as image in a new tab. The picture seems to suggest that the if structure and the loop structure can be reduced. A similar image is present in my professor's slides. "My reading of it suggests that loops add one rather than being" What is the difference? – Cedric Martens Oct 19 '20 at 01:34

1 Answers1

2

Wikipedia spells this out.

What he is trying to answer is how structured a given program is. A Well structured program being one that is expressed by a number of well structured sub-programs.

To describe this convert the program into a control flow graph. Look for sub-graphs corresponding to well structured programs with the requirement of single entry-point, and single exit-point.

After having looked through every node on the graph, and reduced every well structured program, if you are left with a single node you will have a cyclomatic complexity of 1, and it is a structured program (by his definition).

The graph reduction is roughly equivalent to the action of refactoring out some logic into a called function.

Every other program will have a non-reducable graph of more than 1 node. The simplest would be:

    O
    |
  +-+-+
  |   |
  O   O

two exit points, one entry point, and a split between them.


This is particularly useful in languages where a call does not necessarily have to return to the same location such as in Machine Languages, Assembly, and Actor messages.

This analyse is much less useful in languages that have structured programming inbuilt. In these languages all control flow can be reduced down to a single function.

Kain0_0
  • 15,888
  • 16
  • 37