代码质量真的重要吗?
Does Code Quality Even Matter?
原文由官网提供,中文由 GPT-6 Luna 翻译。相邻片段合并排版,正文与时间点保持对应。
已插入 19 张经核对的幻灯片或演示截图;点击图片可放大。图注链接为当前视频时间,左侧正文时间保留原转录版本,二者可能不同。
共 32 个阅读段落

好。今天我想挑个很温和、没人有意见的话题来讲。对吧?社交媒体上也没人讨论过这个。当然,社交媒体上每个人对此发表的看法都特别有深度、面面俱到。大家都有很多深刻的想法。你知道,这件事可能不只是简单的“是”或“否”。
All right. So today I figured I'd pick a nice uncontroversial topic that nobody has any opinions about. Right? Nobody has said anything on this about social media. And of course every take about this on social media is really deeply nuanced. People have lots of depth and really big thoughts. You know, maybe there's a little bit more to it than like yes or no.

当然,如果你问一位资深工程师问题,我们总会对每个问题都给出同一个答案,这是每位资深工程师最爱用的回答:“要看情况。”不过坦白说,这个回答没什么帮助,因为到底取决于什么?我们就从这里开始。代码质量最初为什么会重要?我看到很多人说:“因为它让我感觉很好。”这并不是充分的理由。所以我们看看能不能给这场讨论增添一些细节,至少帮助大家在讨论时有个基础,可以据此推理。我们试着建立一个框架。
And, you know, of course, if you ever ask a senior engineer a question, we always have the same answer to every question, every senior engineer's favorite answer to every question, which is, it depends. Now, that's not a very helpful answer, though, frankly, because what does it depend on? So let's start with this. Why did code quality ever matter in the first place? I see a lot of people be like, well, it made me feel good. That's not really a sufficient justification. So let's see if we can inject some nuance into this conversation and help everybody at least be able to have a basis for reasoning about it while we're discussing it. So let's see if we can make a framework here.
这就是我今天演讲想做的事。
That's what we're going to try to do with this talk.

我们先从一件事说起:你想用软件实现的任何目标,都只取决于两个因素。一是理解,二是基于这种理解采取行动。无论是让模型对齐、扩展系统规模、推动采用,还是其他事情,所有软件的成功都由这两个因素决定。这就是我们需要调试器、可观测性、评测,甚至测试的原因。这些东西都是为了提升你的理解能力,或是基于理解采取行动的能力。这种理解。人们以为它们还有其他用途,但从根本上说,这就是它们存在的原因。我们在代码质量领域做过的一切,唯一目的就是这两件事。
So we're going to start off with everything you want to ever accomplish with a piece of software depends on two factors and two factors alone. And that is understanding and executing on that understanding. Whether that's aligning a model, scaling a system, driving adoption, anything, all software success is determined by these two factors. This is why we have things like debuggers and observability and evals and even tests. All those things actually are there to improve your understanding or ability to execute on that understanding. People think that they're there for other reasons, but fundamentally they're there for that reason. Everything we have ever done in the field of code quality is exclusively and only for these two things.
除此之外别无其他。这和正确性或其他事情无关,只和这两件事有关。
It's not for anything else. And that doesn't have something to do with correctness or something else. It has to do only with these two things.

如果你要当软件工程师,有两方面的理解最重要,这是你必须掌握的。当然,关于软件还有其他方面需要理解,比如你做产品或销售时需要理解的内容。但如果你是软件工程师,最重要的就是这两方面。第一,系统的行为是什么?具体来说,不是文档声称系统会如何运行,而是系统实际会如何运行?第二,我如何正确且安全地修改这种行为?
Now, there's two understandings that are the most important to have if you're going to be a software engineer. There are other understandings to have about software, if you're going to be a product, if you're going to be in sales. But if you're a software engineer, there's the two that are the most important. And the first is, what is the behavior of the system? Like, concretely, not what does a doc say the behavior of the system is, but what is the actual behavior of the system? And the second one is, how do I correctly and safely modify that behavior?

对于一个非常小的系统,如果你想了解它的功能,我直接告诉你它做什么。它会打印“Hello World”。你说:“我希望它说‘Hi World’。”我说:“好,现在我就让它输出‘Hi World’。”除了它具体做什么之外,几乎没什么需要理解的。这就是你需要知道的全部。
On a very small system, if you want to understand what the system does, I just tell you what it does. It prints hello world. You say, I wanted to say hi world. I say, okay, I will make it say hi world now. There's very little to understand about it other than purely what it does. That's all of the understanding that you need.

但任何真正的软件系统都极其复杂。这是一张 Chromium 代码库的真实地图,Chromium 是 Chrome 背后的开源项目。想想这些组件之间可能存在的各种交互方式,它们几乎能产生无限种行为。我不可能用语言向你描述整个 Chromium 系统的行为。
However, any real software system is enormously complex. This is a real map of the code base of Chromium, which is the open source project behind Chrome. And if you think about all of the different ways that these components can interact, they have essentially infinite possible behaviors. Like, I could not describe to you in words the behavior of Chromium as a whole, as a system.

但是,正如大家所知,我们都做过一段时间软件工程师;我可以描述系统中的各个组件。我可以准确描述系统某个组件的行为。这是 Chromium 的 URL 解析器,包括实际函数和相关代码。它会解析 URL 的各个部分。
However, what I can describe to you, and as we all know, everybody's been a software engineer for a while, I can describe to you components of the system. I can successfully describe to you how a component of the system behaves. So this is the URL parser, the actual functions and stuff for the URL parser of Chromium. It figures out all the different parts of the URL.

这就是这段代码存在的全部原因。代码之所以存在,归根结底是为了提供结构。我这是什么意思?左边和右边这两段代码在Linux 机器上运行时,都会打印 Hello World。不过左边的代码一眼就能看出它是 Hello World。它具有 Hello World 的身份和结构。右边则只是一连串指令,没有明确、可识别的或可理解的身份。
And this is the whole reason that code exists. The whole reason that we have code at all in the first place is for structure. What do I mean by that? Both of these two things on the left and on the right will print Hello World if run on a Linux machine. However, on the left you can see that it is Hello World. It has the identity and structure of Hello World. On the right, you just see a series of instructions with no particular identity, recognizable or understandable identity.
它不是一个组件,只是一堆指令。这里的代码还算能理解,但如果这样的代码有数百万或数十亿行,就会难以理解得多。过去我们讨论过可读性、可测试性之类的概念,听起来像是关于代码的人类观念。但真正始终最重要的,是系统具有某种易于理解的结构。所以真正重要的是结构。当你要修改系统时,这一点尤其重要;这就是我们说的第二种理解,也就是如何正确、安全地修改系统。
It's not a component. It's just a bunch of instructions. While that's understandable there, if that was millions or billions of lines of that, it would be much harder to grok. In the past, we've talked about things like readability or testability or things that seemed like human concepts about code. But what actually mattered the most always was that the system had some sort of understandable structure. So it's the structure that actually matters. And when this matters the most is when you want to modify the system, the second understanding that we talked about.

对系统做任何修改,都会带来两类风险。第一类风险是你会引入某种问题。问题不只是 bug,而是任何可能发生的坏事。比如项目所需时间可能远超预期。系统的行为可能不符合你对用户的预期。项目也可能因为预算超支而失败。你只是试图完成这项改动,就可能引发任何种类的问题。第二类风险是你可能让未来的改动更难完成。具体来说,这会增加未来出现问题的概率。你可能提高系统未来的风险。这两类风险都是由理解方面的问题引起的。第一类与一开始理解系统有多困难有关。
All modifications to a system carry two categories of risk. The first is that you will introduce some sort of problem. And a problem isn't just a bug, it's anything bad that could happen. Like your project could take much longer than you expect. It could not behave in the way that you wanted it to for your users. The project could fail because you have budget overruns. Any kind of problem could happen from just you trying to go through the process of making this change. And the second is you could make it harder to make future changes. In particular, what that means is you could make it more likely to have future problems. You could increase future risk of the system. These two things are caused by problems with understanding. So the first has to do with the difficulty of understanding the system in the first place.
第二类则是你让系统变得更难理解。我读过的每一份事故复盘报告,归根结底都可以归结为这两个因素,只是具体表现形式不同。生产环境配置太难理解。有人做出了让人完全无法理解的东西。系统出现了无法理解的涌现特性。诸如此类。第二点会反过来影响第一点,形成恶性循环,并以非线性的方式不断累积。因此,理解难度不会线性增长,而会随时间不断累积。最终你会达到某个阶段,而所有系统最终似乎都会走到这个阶段,每次修改都会引发某种问题。
And the second is you make the system harder to understand. can reduce every post-mortem I have ever read for any incident to these two factors in one way or another. The production configuration was too hard to understand. Somebody made something impossible to understand. There were emergent properties of the system that were impossible to understand. Whatever. Point two feeds into point one in a vicious cycle that compounds in a non-linear fashion. So the difficulty of understanding does not grow linearly, it compounds over time. You will eventually reach a point, and all systems eventually tend to reach this point, where every modification involves some sort of problem.
有些系统会变成根本无法修改的状态。想想那些庞大的100 million 行、500 million 行的主机代码库:任何变更都会带来某种问题。你会在组织问题中看到这种现象,到处都能看到,包括项目进度等等。有人会说,也许未来的模型能解决这个问题。也许它们可以,也许未来的模型能以某种方式无限处理复杂性,以某种方式。但今天我们不必讨论这个,因为我觉得模型现在正在做另一件非常有意思的事。它们正在创造一种新型软件,也就是一个新的软件类别。这个类别一直都存在,但很快它的数量就会达到其他任何软件类型的 100,000 倍,这就是一次性软件。
Some systems get to the point where you can't modify them at, if you think of your giant 100 million, 500 million line mainframe code bases, everything, every change is some sort of problem. You'll see it in organizational issues, you'll see it all over the place, you'll see it in timelines, whatever. And look, there's a lot of arguments about maybe future models will fix this. Look, maybe they will, maybe future models will have infinite ability to deal with complexity somehow. But we don't have to discuss that today because I think models are doing something else really interesting right now. They are creating a new kind of software, a new category of software. This category has always existed, but it's about to become 100,000 times more common than any other form of software, and that is disposable software.
那么,什么是一次性软件?
So what is that?

想象这样一个世界:系统发生故障时,后果微乎其微,甚至完全没有后果。依赖这个系统的人很少,几乎没有其他系统依赖这款软件。变更管理几乎没有,甚至不存在。再想象一下,替换它的成本几乎降到了零。你可以在几小时或几天内替换整个系统。而且维护成本基本为零。在这样的世界里,代码质量不重要。因为我们之前试图防止的那些随时间累积的风险都不存在。
Okay, so imagine a world in which the consequences of failure for your system are very minimal or nonexistent. Imagine that there's very few people that depend on it, and almost no other systems depend on that piece of software. Change management, very minimal, non-existent. Imagine the cost of replacement went to near zero. You could replace the whole thing in hours or days. And it had basically no cost of maintenance. In that world, code quality does not matter. Because all of those things we were trying to prevent about the compounding risks over time don't exist.

传统上,我们一直在构建经久耐用的软件。这类软件能运行多年,被成千上万甚至数百万人依赖,一旦失败就会造成巨大后果,甚至可能给企业带来灾难性后果,而且需要大量维护。世界上大多数软件工程师的大部分时间,都花在维护这些庞大的耐用系统上。我认为这种情况会继续存在。这也是我觉得有时讨论会偏离方向的地方。耐用系统很可能仍是世界上最重要的系统。而且这类系统可能会更多。
Traditionally, we've always built durable software. lives for years, it's depended on millions or thousands of people, there's huge consequences of failure, potentially business catastrophic consequences of failure, and it requires enormous maintenance. Most of the software engineers in the world spend most of their time maintaining these giant durable systems. I expect this to continue to exist. So this is one of the areas where I think sometimes the conversation goes a little astray. Durable systems will continue to probably be the most important systems in the world. And there will be probably more of them.

但数量最多的系统将会是一次性软件。它们可以在几小时内替换。只有少数人会依赖它们。或者说,变更管理根本不重要。如果软件不好用,我们就给每个人配上智能体。他们可以自己生成一个版本。维护会变得无关紧要,或者成本几乎为零。不喜欢操作系统自带的计算器?自己做一个。想给孩子策划一场派对?就为这场派对写一个派对策划软件。用完就扔。代码质量不重要。没人会在意。
But the most numerous systems will be disposable software. They will be replaceable in hours. They will be depended on by a few. Or change management just won't matter. And look, if it doesn't work, we'll give everybody agents. They'll make a version of it themselves. Maintenance will be unimportant or nearly free. Don't like the calculator on your OS? Make your own. Want to plan a party for your kids? Write a party planner software just for that one party. Throw it away. Code quality doesn't matter. Nobody cares.

未来,我们会根据软件的一次性程度来决定采用多严格的工程规范。在左边的情况下,我们所有严谨的工程实践都完全不重要。在右边的情况下,过去几十年积累起来的工程实践都非常重要。
In the future, we will apply engineering rigor to the degree of disposability of the software. On the left, nothing, none of our practices of engineering rigor matter at all. On the right, all the practices of engineering that we have built up over the last many decades matter greatly.

我们可以根据我刚才提到的四个因素,判断你处于这个尺度的什么位置,具体包括:这四个因素是失败后果、替换成本、依赖关系和维护成本。如果这些因素都很低,那就是一次性软件;如果都很高,那就是耐用软件。长期编写软件的人都知道,应该倾向于把软件当作耐用系统来设计。应该假设这些因素都比你预想的更高。
We can determine where you are on this scale by those four factors that I have been talking about. Consequences, cost of replacement, dependencies, and cost of maintenance. If they're all low, it's disposable software. If they're all high, it's durable software. And look, any person who's written software for a long time knows you should err on the side of it being durable. You should err on these all being higher than you expect them to be.

但如今也有一种新做法:让软件变得更容易丢弃。那么这需要做些什么?首先,建立更好的隔离环境。比如规定只能部署到一个只能访问你自己数据的环境中。不要包含客户数据、个人身份信息之类的数据。你可以强制实现这种隔离。你可以规定:其他生产系统不能向这个环境中的系统发起任何入站 API 调用。这样部署到这个环境里的所有东西都是一次性的。冗余。
But there's also a new thing you can do in the world. You can make software more disposable. So, like, what does that involve? Well, come up with better isolated environments. So, hey, you can only deploy to this environment that has access to only your data. No customer data, no personally identifiable information, whatever. You can enforce independence. You can say, okay, no inbound API calls from other production systems are allowed to this system in this environment. And so everything deployed to that environment is disposable. Redundancy.
你可以保留一个老旧笨重、难以使用的备份系统。如果有人想用氛围编程做更高效的前端,那就让他们用氛围编程做自己的高效前端,因为还有一个老旧耐用、笨重的备份系统可供使用,万一他们那糟糕的小型氛围编程作品失效了。它肯定会失效。没关系,失效也不要紧。你也可以限制使用范围,就像我之前说的。它只是给某个人用的计算器,或者只负责策划这一场派对。我们还能做很多事,也可以讨论更多提高软件一次性程度的方法。事实上,我认为这是一门全新的软件工程学科。重点不再是如何提高耐用系统的工程严谨性,而是如何改造系统、鼓励或允许软件更容易被丢弃,让人们可以把这些东西扔掉。
You can have an old clunky backup system that is hard to use. And then, look, if people want to vibe code their own more efficient front ends, you let them vibe code their own more efficient front ends because there is an old, durable, clunky backup system that they can use if their crappy little vibe-coded thing fails. Which it will fail. That's fine. But it doesn't matter if it fails. And you can limit scope, just like I talked about. It's just a calculator for this one person. It just plans this one party. There's so much more we could do here and talk about ways of increasing disposability. In fact, I think this is a whole new discipline of software engineering. Not to think about how we improve the rigor of durable systems, but how do we take our systems and how do we encourage disposability or allow more disposability, allow people to throw these things away.
这个想法很有意思。
Interesting thought.

我想再次强调,耐用软件工程不会消失,因为这是我今天在社交媒体上看到的最大误解。事实上,我相信世界上耐用软件的绝对数量会增加,因为 AI 编程工具让构建更多耐用软件变得越来越容易。
Now, I want to re-emphasize that durable software engineering is not going away because this is the biggest confusion that I see on social media today. In fact, I believe the absolute amount of durable software in the world will increase because AI coding tools make it easier and easier to make more durable software.

另一个重要认识是,一次性软件和耐用软件并非非黑即白。它们处于一个连续区间,就像我之前展示的那样;在这个区间中,变化的是你愿意投入多少精力来遵循严格的工程实践。明白吗?这既包括眼前投入的精力,也包括长期投入的精力,正如我们一直以来讨论软件时所说的,都是如此。不过,我认为最值得关注的是降低投入成本,也就是降低严谨性的成本。比如,如果严谨实践的成本更低、也变得越来越容易做到,你就不必那么担心自己处在这个尺度的什么位置。这是另一种可以采用的方法。
another important thing to understand is that disposable and durable are not black and white. There's a spectrum, like I showed before, but that spectrum, what changes across the spectrum is how much effort you're going to put into engineering rigor. Okay? And that's both immediate effort and effort over time, just like we've always talked about for software. However, what you want to do, and I think what you want to care about the most, is reducing the cost of effort. Reducing the cost of rigor. like if rigor is cheaper if it's the easier and easier you make it to be rigor the less you have to worry about where you are on the scale so this is another approach you can take
如果你改变主意,情况会容易处理得多。你可能会想:“哦,我以为这会是一次性软件,现在它却需要经久耐用。”把软件做得耐用的成本越低,改变主意或承认判断错误就越容易,因为你不可能始终 100% 判断正确。
if you change your mind it's much easier to like you're like oh i thought this was going to be disposable and now it needs to be durable the cheaper you make it to make it durable the easier it is to change your mind or to be wrong because you're never going to be 100 right all the time

因此,这需要另一套实践;它们本身并不属于工程严谨性,而是为了降低工程严谨性的成本。我可以就此讲上好几个小时。我职业生涯中有很大一部分时间都在做这方面的事。但我没有 10 个小时,计时器上只剩大约 5 分钟,所以我尽量讲一些。我们来谈谈标准化。为什么要标准化?有人会说:“我不知道,因为老板这么要求。”其实不是。我们进行标准化,是因为它能减少可能性。如果系统里完全没有 PHP,你就不用弄清楚 PHP 有哪些代码检查工具,有哪些实践,以及需要修复哪些安全漏洞。
on that estimate. So that involves a different set of practices that are not engineering rigor themselves, but that are about how to decrease the cost of engineering rigor. Now, I could talk about this for hours. I have spent a tremendous amount of my career doing this. I don't have 10 hours. I have about five minutes left on that timer. So I'll talk about what I can, okay? Let's talk about standardization. Why do we do standardization? People are like, well, I don't know, because the boss said so. Well, no. We do standardization because it removes possibilities. If you don't have any PHP in your system, you don't have to figure out what are all the linters for PHP, what are all the practices, what are all the security holes I have to fix.
标准化可以直接排除许多可能性,从而降低遵循严谨工程实践的成本。框架也是一样。为什么你不希望用上每一种前端框架,或市面上每一种后端框架?因为这会提高遵循严谨实践的成本。当然,仍然可以做到严谨。你仍然可以让智能体为你编写所有工具,但每次都得重新做一遍。协议也是如此。为什么需要标准化 RPC 协议?原因完全相同。标准之所以存在,都是出于这个原因。明白吗?我把这叫作工作流内工具。工作流内工具就是会自动运行的工具,程序员不需要主动选择运行它。他们不需要理解它如何运行。它会自动发生。
You just eliminate possibilities through standardization, and it just reduces the cost of rigor. Same thing with frameworks. Why don't you want to have every front-end framework or every back-end framework in existence? because it makes rigor more expensive. Like, yes, rigor is still possible. Yes, you could still have the agent code up all your tools, but you have to do it every single time. You know, protocols, why do you want to have standardized RPC protocols? All the same thing. Standards are all for this reason. Okay? What I call in-workflow tooling. So in-workflow tooling is tooling that just occurs. The programmer doesn't have to choose to run it. They don't have to understand how it runs. It just happens.
只要它已经在那里,就感觉像是免费的,而且可靠。可靠性非常非常重要。它不会产生大量误报。它运行得很快,也会自动执行。如果这些条件都满足,你也可以把它应用到一次性软件上。如果它既免费又快速,谁会在意呢?让测试更容易。所以这不只是说:“我们怎样把测试做得更好?”而是,怎样才能让没人需要费心琢磨如何改进测试?怎样才能让每个测试都快速而且结果确定,不必让任何人成为测试方面的专家?要做到这一点,你得在测试基础设施上投入大量工作。比如,我一直有个梦想,作为测试负责人,我希望每个人都能提供自己服务的内存假实现,这样我就可以直接运行这些内存中的这些内存中的服务假实现。
If it's there, it feels and seems free, and it is reliable. Reliability is very, very important. It doesn't have a ton of false positives. It's fast. It just happens. And the more that's true, you can apply it to your disposable software too. If it's free and fast, who cares? Making testing easier. So this is not just about, like, hey, how do we do better testing? Look, how do you make it so that nobody has to think about how to do better testing? How do you make it so that every test is fast and deterministic without anybody having to be a deep expert in testing? There's a ton of work that you have to put into test infrastructure to make that possible. For example, one of the great dreams that I've always had as a test owner is that everybody would provide in-memory fake versions of their services and I could just run those in-memory fake versions of their services.
猜猜怎么着?编程智能体就能做到。所以你现在只要这么做就行。但你必须主动选择这么做,并让所有人都能用。还有一点:要尽可能严格。把所有地方的严格程度都调高。把所有代码检查规则都设为最高严格级别。任何地方都不要妥协。这会消除很多问题,和标准化非常相似。它能省掉大量讨论。有时代码检查工具会判断错误。只要误报率相对较低,不会带来灾难性后果,那就没关系,因为这样一来,未来就会少掉一大堆代码检查错误或其他你本来需要处理的问题。问问任何一个曾经想在一百万行代码库里突然启用代码检查工具的人。
Guess what? The coding agent can do that. So you can just do that now. But you have to choose to do it and make it available to everybody. And another thing is, look, maximum strictness. You just turn the strictness up everywhere. You set all your lint rules to maximum. You never compromise on anything. And it eliminates, it's very similar to standardization. It eliminates a ton of conversations. Sometimes the linter is wrong. As long as the false positive rate is relatively low and doesn't have catastrophic consequences, it doesn't matter because all of a sudden there's a bunch of future lint errors or future issues that you don't have to deal with. Ask anybody who's been like, oh, I suddenly want to turn on my linter in this million line code base.
他们会说:“哇,现在我得做一个 100,000 行的改动。”一开始就把它启用并给所有人使用,要容易得多。我刚才说过,我真的可以站在这里连续讲10 个小时。这方面还有很多很多可以做的事情。我这里只谈了几个方面,但这也是软件工程实践中的一个完整领域,你可以沿着另一个维度应用它。
And they're like, oh, wow. Now here's my 100,000 line change that I need to make. Much easier to do that up front and have it on for everybody. Now look, like I said, I could literally stand here and talk to you about this straight for ten hours. There is much, much more you can do here. This is just a few areas, but also a whole area of software engineering discipline that you can apply on the other axis.

这样我们就能回答最初的问题,不再只说“要看情况”,而是能回答“是”或“否”。代码质量到底重要吗?对一次性软件来说,不重要。对耐用软件来说,重要。我们还可以回答第三个、更有意思的问题:这一定得很贵吗?不必。非常感谢。
So this lets us now answer our question, not just it depends, but yes or no. So does code quality even matter? For disposable software, no. For durable software, yes. And we get to answer a third and even more interesting question, which is, does it have to be expensive? No. Thank you very much.