Linus Torvalds on Cognitive Load: Writing Understandable Code

software engineering

Linus Torvalds critiques code that increases cognitive load, advocating for clear, direct implementations over needless abstractions. This enhances readability and maintainability for humans and AI developers alike.

Linus Torvalds, the renowned creator of Git and Linux, recently criticized a pull request submitted by a Meta engineer, labeling the proposed code as "garbage." His critique centered on a helper function, make_u32_from_two_u16(), introduced into generic header files. Torvalds argued that such an abstraction actively degrades code quality, making it incomprehensible and worse than simply writing out the operation directly.

He emphasized that explicit code like (a << 16) + b immediately conveys its intent and word order, even if it requires a minor cast for correctness. In contrast, make_u32_from_two_u16(a,b) leaves the user without a clear understanding of the word order, effectively making the code worse and introducing unnecessary complexity into generic files.

Optimizing for Reduced Cognitive Load

Torvalds' core message underscores a critical principle in software development: good code optimizes for reducing cognitive load. This principle is increasingly vital given that modern code has multiple readers—humans, Large Language Models (LLMs), and computers.

Both humans and LLMs have finite "context windows" for information processing. Needing to navigate to a new file or function to grasp its purpose incurs a "micro-context switch," demanding extra mental effort (and tokens for LLMs). A function utilizing zero helper functions requires less context and brainpower than one with several layers of abstraction. Neuroscience supports this, showing that task switching incurs measurable brain energy costs. Each abstraction or helper function consumes valuable working memory "chunks," leading to potential cognitive overload and increasing the chance of errors in both human and AI comprehension.

The "Yes, Please Repeat Yourself" (PRY) Principle

This perspective often means that duplication can sometimes reduce cognitive load, as a self-contained code block or "chunk" is easier to process. While abstractions are justified for enforcing consistent behavior across a codebase, simple, reused operations throughout the code can often remain duplicated for clarity. The question developers should ask is: "Do you really need that helper file or abstracted class?"

Embracing PRY can lead to clearer, more understandable code that is easier to iterate on. Pointing AI coding agents like Claude Code or Codex to a single, self-contained file for changes is far more efficient than requiring them to traverse multiple files to understand the full context. This aligns with the concept of "cognitive locality of reference," where keeping related code close together enhances comprehension and efficiency.

Furthermore, the cost of premature optimization, particularly excessive abstraction, is higher than ever. Modern IDEs and advanced LLMs have made code refactoring significantly cheaper, diminishing the perceived cost of duplication.

Professionalism in Communication

While Linus Torvalds' technical insights are invaluable, it's important to not emulate his often harsh and rude tone. Professional and polite communication fosters a collaborative environment, encouraging developers to contribute without fear of public beratement.

The ultimate goal remains: optimize for reducing cognitive load in all code.