Cognitive Load
In the context of software engineering, cognitive load refers to the amount of mental effort required to perform a task, such as understanding what a code does, adding a new feature, or fixing bugs. This mental effort can be categorized into two main types:
- Intrinsic: This is the inherent difficulty of the task or the problem itself. For example, working in a complex banking domain comes with its own set of challenges that cannot be simplified: we can’t make the domain simpler. Intrinsic cognitive load is the built-in difficulty tied to the task.
- Extraneous: This load is caused by the way information is presented and is imposed by factors not directly relevant to the problem we are trying to solve. For instance, regardless of the domain complexity, a poorly designed API with inconsistent levels of abstraction increases extraneous cognitive load, as it complicates the process without adding value.
Understanding the difference between intrinsic and extraneous cognitive load is crucial. While the intrinsic load is unavoidable, the extraneous load can and should be minimized. As software engineers, our goal is to make tasks as straightforward as possible, reducing unnecessary mental effort.
Our working memory capacity (the part that holds temporary information) is significantly limited compared to machines. Try to rewrite even a 20-line function from memory (with the same variable names, etc.). It’s a challenging task because our working memory capacity is limited.
Here are five key areas to manage cognitive load effectively:
- Code complexity: Apply best practices to make our code as readable as possible. This includes reducing nested code, simplifying conditions, limiting inheritance levels, ensuring code cohesion, etc.
- Abstractions: Be cautious with abstractions. Avoid unnecessary or premature abstractions that complicate the code without providing clear benefits. Ensure that abstractions add value and are used consistently.
- Documentation: Maintain clear, concise, and up-to-date documentation. This includes all the assets that are supposed to enhance our understanding of a codebase or systems, such as README files, code comments, and tests (yes, tests are also documentation). Good documentation helps reduce extraneous cognitive load by making information easy to access and understand.
- Communication: Ensure that communication within our team is efficient and effective. Clear discussions about design and task requirements can prevent misunderstandings and reduce cognitive load.
- Context switching: Frequent task switching can negatively impact cognitive load — debugging, meetings, code reviews, and more. Let’s try to minimize interruptions and not hesitate to communicate with our manager if frequent context shifts are affecting our productivity.
In summary, it’s essential to understand the two types of cognitive load: intrinsic and extraneous. Instead of attributing complexity solely to the inherent difficulty of a domain — such as saying, “Our codebase is complicated because the domain is complicated” — we should focus on reducing extraneous cognitive load. Our responsibility is to simplify and improve the clarity of our code and processes, hence mitigating unnecessary mental efforts.
This post is taken from The Coder Cafe newsletter (issue #4).
Feeling overwhelmed by the endless stream of tech content? At The Coder Cafe, we serve one essential concept for coders daily. Written by a senior software engineer at Google, it’s perfectly brewed for your morning coffee, helping you grow your skills deeply.