2

From Sonar Metrics complexity page the following method has a complexity of 5.

public void process(Car myCar){          <- +1
        if(myCar.isNotMine()){               <- +1
             return;                         <- +1
        }
        car.paint("red");
        car.changeWheel();
        while(car.hasGazol() && car.getDriver().isNotStressed()){   <- +2
             car.drive();
        }
        return;
    }

This is how the tool calculate complexity:

Keywords incrementing the complexity: if, for, while, case, catch, throw, return (that is not the last statement of a method), &&, ||, ?

Why do case statements,if blocks and while blocks increase the complexity of the method? What is the intuition behind this metric calculation of complexity of methods?

wsdookadr
  • 2,584
  • 1
  • 21
  • 44
Geek
  • 26,489
  • 43
  • 149
  • 227
  • Because they do comparisons. `default` `finally` don't because they don't do comparisons, because comparisons are done before. – shuangwhywhy Mar 06 '13 at 17:42

3 Answers3

2

It's because they have conditions in them which increase the number of tests needed to ensure that the code is correct.

Also probably ifs have less complexity than loops (while, for). Also read up on cyclomatic complexity related to this.

Read this blog post, it describes the actual reality of not being able to test everything and the sheer number of tests you require to test everything.

wsdookadr
  • 2,584
  • 1
  • 21
  • 44
1

Maybe it is based on the Cyclomatic Complexity by McCabe (at least looks like it).
This metric is widely used in the Software Engineering field.
Take a look at this: http://en.wikipedia.org/wiki/Cyclomatic_complexity

Giovani Guizzo
  • 527
  • 3
  • 13
  • Because it is a trustworthy metric for complexity measurement. Most of the papers in the Software Engineering field use it and it is proven to be valid. I advise you to read McCabe paper for a better understanding, or take a look at this: http://www.guru99.com/cyclomatic-complexity.html – Giovani Guizzo Mar 06 '13 at 17:45
1

Somar measures cyclomatic complexity, which represents the number of linearly independent paths through the source code.

The key to answering your question comes from a research paper of Thomas McCabe, published in December of 1976:

It can be shown that the cyclomatic complexity of any structured program with only one entrance point and one exit point is equal to the number of decision points (i.e., 'if' statements or conditional loops) contained in that program plus one.

This is precisely what Sonar does: it finds the decision points, which come from loops, conditional statements, and multipart boolean expressions, and counts their number.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • What do you mean by linearly independent paths? – Geek Mar 06 '13 at 18:36
  • @Geek If you represent your program as a graph of undirected edges that connect decision points, the cycles that contain no other cycles are considered linearly independent paths through the code. The description section of the wiki page linked from the answer has nice pictures to show what it means. – Sergey Kalinichenko Mar 06 '13 at 18:42