Clean Code: Cognitive Complexity by SonarQube

Himanshu Ganglani
2 min readAug 24, 2023

--

Cognitive Complexity is a measurement of how difficult it is to understand a specific piece of code. Unlike Cyclomatic Complexity, which primarily focuses on control flow based on branches and decisions, Cognitive Complexity takes into account the cognitive load required to comprehend the code. It considers factors such as nested conditionals, loop complexity, and the overall structure of the code. goal is to keep the Cognitive Complexity of methods and functions as low as possible while maintaining code clarity and readability. A general rule of thumb is to aim for Cognitive Complexity scores below 15.

Cognitive Complexity scores vary based on the specific constructs used within your code. It takes into account factors like nesting, logical operations, and conditions. The higher the Cognitive Complexity score, the more complex and harder to understand the code becomes.

While there’s no fixed number that is considered “allowed,” it’s recommended to keep your codebase’s average Cognitive Complexity within a reasonable range. Maintaining an average score below 10 or 15 is a good practice, indicating that your code is relatively easy to comprehend.

Cognitive Complexity Metrics Calculation:

The Cognitive Complexity metric takes into account the following factors:

  • Nesting: Deeper nesting of control structures like conditionals and loops increases cognitive complexity.
function nestedConditionalsExample(a, b, c) {
if (a) {
if (b) {
if (c) {
// Do something
}
}
}
}
  • Logical Operators: Complex boolean expressions with multiple logical operators contribute to higher cognitive complexity.
function complexBooleanExample(x, y, z) {
if ((x && y) || (y && z) || (x && z)) {
// Do something
}
}
  • Number of Conditions: An excessive number of conditions within a single statement or method elevates cognitive complexity.
function excessiveConditionsExample(a, b, c, d, e) {
if (a && b && c && d && e) {
// Do something
}
}

Interpreting Cognitive Complexity Scores:

SonarQube provides a numerical score for Cognitive Complexity. Each function or code block is assigned a score based on its complexity. Higher scores indicate more challenging code to understand.

Best Practices for Managing Cognitive Complexity:

  • Simplify Logic: Break down complex logic into smaller functions or methods to enhance readability.
  • Reduce Nesting: Avoid deep nesting of conditionals and loops.
  • Extract Methods: Create separate methods for complex code segments to reduce cognitive complexity.
  • Limit Conditions: Refactor code to avoid excessive conditions within a single statement.

7. Conclusion:

Cognitive Complexity, a key metric in SonarQube, assesses the difficulty of understanding code. Through JavaScript examples, we’ve seen how nested conditionals, complex boolean expressions, and excessive conditions contribute to higher cognitive complexity. By actively managing cognitive complexity and following best practices, developers can ensure their code remains clear, understandable, and maintainable, leading to improved code quality and efficient collaboration.

--

--

Himanshu Ganglani

Software Engineer with 5 year of experience in Javascript, Node.Js, AWS, typescript, Docker.