Skip to content

Adding Understandability (Sonar Cognitive Complexity)#74

Merged
floriankraemer merged 6 commits into
masterfrom
sonar
May 25, 2026
Merged

Adding Understandability (Sonar Cognitive Complexity)#74
floriankraemer merged 6 commits into
masterfrom
sonar

Conversation

@floriankraemer
Copy link
Copy Markdown
Contributor

Understandability measures how hard it is for a human to follow a method’s control flow. It implements Sonar Cognitive Complexity as described in SonarSource’s 2023 white paper (Cognitive Complexity: a new way of measuring understandability, v1.7).

This metric is separate from this tool’s weighted Cognitive Complexity score, which sums logarithmic weights over structural metrics (lines, arguments, if count, and so on). Understandability follows Sonar’s rule-based control-flow model instead.

This commit introduces a new set of metrics focused on understandability, enhancing the cognitive analysis capabilities. Key changes include:

- Added `UnderstandabilityMetrics` and `UnderstandabilityCalculator` for calculating and summarizing understandability metrics.
- Integrated understandability metrics into the existing cognitive metrics framework.
- Updated configuration options to enable the display of understandability metrics in reports.
- Enhanced the reporting system to include understandability in the output format.
- Added tests to ensure the correctness of the new understandability features.

These changes aim to provide deeper insights into code complexity and maintainability, aligning with modern coding standards.
- Added new methods in `UnderstandabilityMetrics` for resolving risk levels and count values, improving data handling and robustness.
- Updated `Parser` class to include understandability metrics in the analysis process.
- Enhanced `CombinedMetricsVisitor` with type annotations for the `getMethodUnderstandability` method.
- Added suppression warnings in `UnderstandabilityVisitor` for excessive complexity and method count, promoting cleaner code practices.
- Introduced a new document, Understandability.md, detailing the Sonar Cognitive Complexity metric.
- Explained the calculation method, risk levels, and configuration options for enabling understandability metrics.
- Provided references to relevant literature and resources for further reading on cognitive complexity.
…d clarity

- Updated `UnderstandabilityCalculator` to skip low complexity methods, enhancing risk categorization.
- Refined `UnderstandabilityVisitor` logic to return early for ignored classes and non-relevant nodes, improving code readability and maintainability.
- Adjusted nesting and logical operator handling to streamline flow and reduce unnecessary checks.
…rstandability output

- Introduced a new constant `CACHE_VERSION` in `CognitiveMetricsCollector` to manage cache versioning effectively.
- Updated cache item versioning to use the new constant, ensuring stale entries are ignored when the analysis result shape changes.
- Enhanced the understandability output in various test files by updating complexity ratings, improving clarity in metrics reporting.
@floriankraemer floriankraemer merged commit f2a0fc7 into master May 25, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant