Enhancing Code Quality: The Importance of Cognitive Complexity
Written on
Chapter 1: The Need for Measuring Code Understandability
In the realm of programming, we frequently assess how long it takes for code to execute and how much memory it consumes. However, how often do we evaluate how easily our code can be comprehended by a human? Likely, not as frequently as we should. High-level programming languages were developed to enhance human readability, emphasizing the importance of code understandability. But what methods can we employ to quantify this attribute?
Ann Campbell from SonarSource has introduced a metric known as cognitive complexity, which serves as a tool for gauging the understandability of code.
Measuring Cognitive Complexity
Code can become challenging to grasp for various reasons. The primary factors identified by Campbell include:
- Interruptions in the linear progression of code
- Nested "flow-breaking structures"
We will delve into the implications of these points shortly. It's also crucial to note that, as per Campbell's criteria, code does not inherently become more or less complex based solely on the use of shorthand commands provided by the programming language.
For instance, consider the following Python code that determines the larger of two numbers.
# Example code for finding the greater of two numbers
a = 10
b = 20
greater = a if a > b else b
This snippet is deemed as cognitively complex as the following equivalent code.
# Another way to find the greater number
if a > b:
greater = aelse:
greater = b
With this clarification in mind, let's revisit the two elements that contribute to cognitive complexity, as outlined in Campbell's framework.
Section 1.1: Interruptions in Linear Code Flow
The term "linear flow of code" refers to a seamless execution from the start to the end of the code. Interruptions in this flow can arise from various structures that compel readers to navigate back and forth, rendering the code more difficult to interpret. Here are some features that can disrupt the flow:
- Loops
- Conditionals
- Exception handling
- Switch statements
- Chains of logical operators
- Recursion
- Goto statements
Subsection 1.1.1: Nested Flow-Breaking Structures
Nested constructs demand more mental processing from the reader and naturally contribute to increased cognitive complexity. Conditionals, loops, and exception handling structures can all qualify as nested flow-breaking components, thus elevating cognitive complexity.
Section 1.2: Tools for Calculating Cognitive Complexity
Fortunately, you need not manually calculate the cognitive complexity of your code. Various plugins and extensions are available to assist you, such as the one shown below.
Screenshot from jetbrains.com
Chapter 2: Strategies for Reducing Cognitive Complexity
In the video titled Refactoring with Cognitive Complexity, viewers will learn how to effectively manage and minimize cognitive complexity in their code. The presentation highlights strategies and practical tips for enhancing code readability.
The second video, Reka/Ben: Actionable Insights vs. Ranking, discusses the appropriate and inappropriate uses of code quality metrics, providing insights on how to leverage these tools for better coding practices.
Conclusion
To enhance our coding practices, it's essential to focus on reducing cognitive complexity. This can be achieved by ensuring a linear flow in our code and minimizing the use of nested structures. Thank you for your attention!
For further reading on cognitive complexity and to gain a deeper understanding, you can check out this document authored by Campbell.
For insights on time complexity, consider exploring this article:
Introduction to Time Complexity and Big O Notation
A crash course in time complexity and working with Big O Notation in preparation for your next technical interview or…
javascript.plainenglish.io