Details

Type: MMF

Status: Closed

Priority: Major

Resolution: Fixed

Labels:
Description
The Problem
The way the Cyclomatic Complexity rule currently computes the value incorrectly in all language plugins; the rules combine computation of the Cyclomatic Complexity and Essential Complexity.
As defined in the two following documents [1][2], the computation formula should be either (same result) :
 CC = E − N + 2P where
 E = the number of edges of the graph.
 N = the number of nodes of the graph.
 P = the number of connected components.
 or for each method/function definition: number of conditional branches + 1 which means sum('if', 'for', '&&', '', ternary operator, 'while', 'for', 'case') + 1
 Lambdas should be considered as an entry point
However, currently unconditional branches like 'return', 'break', 'continue', ... are also incrementing the value.
The Fix
The goal of this MMF should not be to fix the known limitations of the "Cyclomatic Complexity" measure, but to stick as much as possible to the theorical definition, so:
 The exception handling mechanisms in modern languages like java, C#, C++, ... should not be taken into account while computing Cyclomatic Complexity. This means for example that in Java that the 'catch' and 'throw' statements should be ignored.
 Cyclomatic Complexity must be computed on every method even if some methods can be considered as accessors in some languages (relates to
SONAR5224,SONARJAVA398, ...).  In the case of switch statements, the Cyclomatic Complexity can indeed be very high, and therefore perceived by developers as a kind of FP
 In the case of deeply nested control flow statements, the Cyclomatic Complexity can be pretty low versus the complexity perceived by developers
 There is a strong correlation between Cyclomatic Complexity and LoC.
The Details
 In each language the implementation of the Cyclomatic Complexity rule(s) should be corrected to adhere to the definition of Cyclomatic Complexity. That is, increments for return, break, continue, and so on should be removed.
 In languages with static initializers, such an initializers complexity score should be added to the class complexity score.
 Because the Cyclomatic Complexity of a class or application largely corresponds to LoC, those metrics aren't helpful, and should be deprecated/hidden.
[1]  http://www.mccabe.com/pdf/mccabenist235r.pdf
[2]  https://en.wikipedia.org/wiki/Cyclomatic_complexity
Attachments
Issue Links
 breaks down into

SONARTSQL65 Compute the Cyclomatic Complexity Metric
 Open

SONARABAP302 Correct method Cyclomatic Complexity rule S1541
 Closed

SONARABAP303 Deprecate rule: S1311
 Closed

SONARJAVA75 Correct method Cyclomatic Complexity rule: MethodCyclomaticComplexity
 Closed

CPP1679 Deprecate Cyclomatic Complexity rules for scopes > method
 Closed

CPP1680 Correct Cyclomatic Complexity implementation
 Closed

SONAR8331 Clean up complexity metrics
 Closed

SONARCOBOL1369 Correct Cyclomatic Complexity rules
 Closed

SONARCOBOL1370 Deprecate rule: COBOL.ProgramComplexityCheck
 Closed

SONARFLEX85 Correct Cyclomatic Complexity rule FunctionComplexity
 Closed

SONARFLEX86 Deprecate rule: ClassComplexity
 Closed

SONARJAVA357 Stop excluding getters/setters from cyclomatic complexity computation
 Closed

SONARJAVA1921 Deprecate rule: ClassCyclomaticComplexity
 Closed

SONARPHP664 Correct Cyclomatic Complexity implementation
 Closed

SONARPHP665 Deprecate rule: S1311
 Closed

SONARPLSQL564 Correct Cyclomatic Complexity implementation
 Closed

SONARPLSQL565 Deprecate rule: PlSql.FileComplexity
 Closed

SONARPY180 Correct Cyclomatic Complexity
 Closed

SONARPY181 Deprecate rule: ClassComplexity
 Closed

SONARRPG145 Correct Cyclomatic Complexity implementation
 Closed

SONARSWIFT243 Correct Cyclomatic Complexity rule S1541
 Closed

SONARSWIFT244 Deprecate Cyclomatic Complexity rules at level > method
 Closed
 is related to

MMF760 PHP: Cognitive Complexity
 Closed
 relates to

SONAR4857 Introduce new "Essential Complexity" core metrics
 Closed

SONAR5812 Remove the "accessors" metric
 Closed

MMF548 Provide a metric that measures the difficulty to understand a method (Cognitive Complexity)
 Closed