SonarSource
Cognitive Complexity Measurement Framework
Pages
22
Time to read
24 mins
Publication
Language
English
Pages
22
Time to read
24 mins
Publication
Language
English
This white paper presents a new metric called Cognitive Complexity, aimed at improving the measurement of code understandability compared to Cyclomatic Complexity. Cyclomatic Complexity has been the standard for assessing the testability and maintainability of software modules but has limitations in evaluating understandability. The paper outlines the shortcomings of Cyclomatic Complexity, particularly its inability to account for modern programming constructs and its failure to provide meaningful assessments at the class and application levels. Cognitive Complexity introduces a set of rules for scoring that aligns more closely with programmer intuition regarding the cognitive effort required to understand code. The paper details the basic criteria and methodology for calculating Cognitive Complexity scores, including increments for various control flow structures, such as loops and conditionals, while ignoring shorthand structures that enhance readability. The proposed metric aims to incentivize better coding practices by focusing on the mental effort required for code comprehension.