search menu icon-carat-right cmu-wordmark

Refactoring with Cognitive Complexity: The New Option for Measuring Understandability

May 2017 Presentation
G. Ann Campbell (SonarSource SA)

Cyclomatic complexity measures testability, not understandability. Learn how to use cognitive complexity in refactoring for simpler, more maintainable code.

Publisher:

Software Engineering Institute

Watch

Abstract

For 40 years, cyclomatic complexity has been the de facto standard for measuring testability and maintainability. While it's absolutely accurate for testability, the same can't be said for maintainability. Cognitive complexity solves the problem and returns "fair" relative measurements of control flow complexity. In this session, you'll learn what the cognitive complexity methodology is and why you should add it to your metric suite. You’ll see how it applies to a number of code samples from open-source projects on SonarQube.com. And you'll learn how to use a method's cognitive complexity score as a guide to designing better code and refactoring existing code to be simpler and more maintainable. Because there's no "cost of entry," high class-level cognitive complexity is a reliable indicator that a class contains a lot of logic, not just a lot of methods. That is, you can use cognitive complexity to sift domain classes from the logic-intense classes that impose a higher maintenance burden. Cognitive complexity is already available as a new rule in some Sonar code analyzers and should be available throughout the SonarQube ecosystem by the end of 2017.