Difference between "complexity" metric and "complexity / method" metric

15,300

You can have a look at our "Metric Definitions" Wiki page (and more specifically on the "Complexity" section) to know more about the meaning of metrics found in Sonar.

  • "Complexity" is the total complexity of a resource (for a file => sum of the complexities of every class defined in the file)
  • "Complexity / method" is the "Complexity" divided by the number of methods found in the file
  • ... and so on for the other metrics.
Share:
15,300
Jérôme Radix
Author by

Jérôme Radix

Updated on July 07, 2022

Comments

  • Jérôme Radix
    Jérôme Radix almost 2 years

    In Sonar, for a particular Java class, I see :

    Complexity:     830
    
    Complexity /method:     8,1
    

    How could you explain the difference between those two metrics ?

    Is "Complexity" the class complexity ?

    What is the maximum complexity a class should be under ? I know methods should be less than 10 for method complexity, but what is the maximum class complexity number a class should conform to be considered not complex ?

    Thanks.

    EDIT

    I also look at Cyclomatic-Complexity rule violations in the file.

    For a number of methods, sonar says with different complexity numbers (in french) :

    "La complexité cyclomatique de la classe est de 28 alors que le maximum autorisé est de 10."

    in english :

    "The class cyclomatic complexity is 28 but the maximum allowed is 10".

    I don't understand why it says "class cyclomatic complexity" as it seems as a "method cyclomatic complexity".

    Also, the class in question does not appears on top of the most complex class sorted by average complexity/method, as sonar says it has only 8,1 average method complexity.

  • Jérôme Radix
    Jérôme Radix about 12 years
    Thanks for your answer. Perhaps it is me but it was not that obvious that "Complexity" means "sum of methods complexities", but now it seems obvious indeed. The wiki page does not state that it is the sum of all methods complexities.
  • Tibor Bakota - FrontEndART
    Tibor Bakota - FrontEndART over 9 years
    @Jérôme: Officially, the sum of McCabe's complexities for a class is called WMC (Weighted Methods per Class) as described here.
  • XtianGIS
    XtianGIS over 7 years
    the link presented does not differentiate among the complexity metrics that sonar handle: like CoreMetrics.FILE_COMPLEXITY and CoreMetrics.COMPLEXITY