认知负荷才是关键

The logo image was taken from Reddit.

标志图片取自Reddit。

It is a living document, last update: January 2025. Your contributions are welcome!

这是一个活文档,最后更新:2025年1月。欢迎你的贡献!

Introduction

介绍

There are so many buzzwords and best practices out there, but most of them have failed. We need something more fundamental, something that can't be wrong.

有很多流行词和最佳实践,但大多数都失败了。我们需要一些更基本的东西,一些不会出错的东西。

Sometimes we feel confusion going through the code. Confusion costs time and money. Confusion is caused by high cognitive load. It's not some fancy abstract concept, but rather a fundamental human constraint. It's not imagined, it's there and we can feel it.

有时我们在阅读代码时会感到困惑。困惑会浪费时间和金钱。困惑是由高 认知负荷 引起的。这不是一些花哨的抽象概念,而是 一个基本的人类限制。它不是想象出来的,它确实存在,我们可以感受到它。

Since we spend far more time reading and understanding code than writing it, we should constantly ask ourselves whether we are embedding excessive cognitive load into our code.

由于我们花费更多的时间在阅读和理解代码上,而不是编写代码上,我们应该不断问自己是否在代码中嵌入了过多的认知负担。

Cognitive load

认知负荷

Cognitive load is how much a developer needs to think in order to complete a task.

认知负荷是开发人员完成任务时需要思考的量。

When reading code, you put things like values of variables, control flow logic and call sequences into your head. The average person can hold roughly four such chunks in working memory. Once the cognitive load reaches this threshold, it becomes much harder to understand things.

阅读代码时,你会将变量的值、控制流逻辑和调用顺序等内容放入脑中。普通人可以在工作记忆中大约保持四个这样的块。一旦认知负荷达到这个阈值,理解事物就会变得更加困难。

Let's say we have been asked to make some fixes to a completely unfamiliar project. We were told that a really smart developer had contributed to it. Lots of cool architectures, fancy libraries and trendy technologies were used. In other words, the author had created a high cognitive load for us.

假设我们被要求对一个完全不熟悉的项目进行一些修复。我们被告知一个非常聪明的开发人员参与了这个项目。使用了很多很酷的架构、花哨的库和流行的技术。换句话说,作者为我们创造了很高的认知负荷。

Cognitive Load

We should reduce the cognitive load in our projects as much as possible.

我们应该尽可能减少项目中的认知负荷。

Types of cognitive load

认知负荷的类型

Intrinsic - caused by the inherent difficulty of a task. It can't be reduced, it's at ...

开通本站会员,查看完整译文。

Home - Wiki
Copyright © 2011-2025 iteam. Current version is 2.139.1. UTC+08:00, 2025-01-13 19:49
浙ICP备14020137号-1 $Map of visitor$