Design for Understandability
本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。
Design for Understandability
This is a talk I gave at Google’s internal Code Health Summit in November of 2025. I’m sharing it here in the hopes that it sparks further learning and discussion. Abstract In a world where LLMs generate much of our code, understanding that code is more important than ever. But with a system as large and complex as Google’s codebase, SWEs can easily fall into a pattern of learned helplessness where the best you can do is tweak the code and pray that it passes the tests. Without understanding, LLMs are little more than complexity-generating machines running at top speed. There’s a better way to go about this. Just as our industry has learned to design software for testability, we can also design it for understandability. We can use ideas like design by contract, class invariants, modularity, and more to create systems that we can understand one piece at a time, rather than piling more onto the big ball of mud and hoping it doesn’t all collapse. Is it easy? No. But is it worthwhile? Absolutely. These ideas aren’t new, but they’re not nearly as widely taught or well understood as they deserve to be, and mastering them leads to a world where we can have more confidence in our code and increase velocity at the same time.
这是我在 2025 年 11 月于谷歌内部代码健康峰会上发表的演讲。我将其分享在此,希望能激发进一步的学习和讨论。摘要:在一个大语言模型(LLM)生成大量代码的世界里,理解这些代码比以往任何时候都更加重要。然而,面对像谷歌代码库这样庞大而复杂的系统,软件工程师(SWE)很容易陷入一种习得性无助的模式,即你所能做的最好的事情就是调整代码,然后祈祷它能通过测试。如果没有理解,LLM 不过是高速运转的复杂性生成机器。我们有更好的方法。正如我们的行业已经学会了为可测试性而设计软件一样,我们也可以为可理解性而设计。我们可以利用契约式设计、类不变量、模块化等理念,构建出我们可以逐个部分去理解的系统,而不是在“大泥球”上不断堆砌,并祈祷它不会崩塌。这容易吗?不容易。但它值得吗?绝对值得。这些理念并不新鲜,但它们远未得到应有的广泛传授或深入理解,而掌握它们将引领我们进入一个能够对代码更有信心并同时提高开发速度的世界。
Slides and Transcript Hi everyone, thanks for having me! Let’s get started. I’d like to start this talk with a question: If LLMs can code, then what is left for human SWEs? The tech industry is quickly looking to large language models to write all of our code. But you might say: isn’t writing code our job as software engineers? Where does this leave the responsibility of SWEs? What is the essential human element that SWEs add on top of LLMs? Every one of you is going to have a different answer to this question, and that’s fine. But here’s my take. That necessary human element is judgment. Judgment of whether the code is maintainable. Judgment of whether it is healthy. LLMs can follow patterns and generate code that looks like what it was trained on. Sometimes they can produce useful tips and critiques for a piece of code. But that critical piece of judgment, of deciding whether some code is suitable for the task it was designed for, is still up to you, the human. Because here’s the thing: LLMs today are still a sloppy intern. Sometimes the code they produce is good, but sometimes it’s completely off-track and needs correcting. You wouldn’t incorporate a sloppy intern’s code into your system without a close review first, so you shouldn’t do so for an LLM’s code either. YOU are responsible for the quality of your code, regardless of whether you had an LLM generate it for you or wrote it the old-fashioned way with fingers on a keyboard
幻灯片与演讲稿:大家好,感谢邀请!让我们开始吧。我想以一个问题开启这次演讲:如果 LLM 可以编写代码,那么人类软件工程师还剩下什么工作?科技行业正迅速转向利用大语言模型来编写我们所有的代码。但你可能会说:编写代码难道不是我们软件工程师的工作吗?这让软件工程师的责任何在?软件工程师在 LLM 之上所添加的核心人类要素是什么?你们每个人对这个问题都会有不同的答案,这没关系。但我的看法是:必要的人类要素是判断力。判断代码是否可维护,判断代码是否健康。LLM 可以遵循模式并生成看起来像其训练数据的代码。有时它们能为一段代码提供有用的建议和批评。但那个关键的判断环节——即决定某段代码是否适合其设计任务——仍然取决于你,也就是人类。因为事实是:今天的 LLM 仍然是一个粗心的实习生。有时它们生成的代码很好,但有时却完全偏离轨道,需要修正。你不会在没有仔细审查的情况下将粗心实习生的代码合并到你的系统中,所以你也不应该对 LLM 的代码这样做。无论你是让 LLM 为你生成代码,还是用老式方法亲手敲击键盘编写代码,你都要对代码的质量负责。
So where does that leave us? If LLMs are going to write a lot of the code for us, then we’re looking at a future where we humans are going to spend less time writing code, and more time reading code and understanding it, in order to make sure it passes our quality bar. This means it’s more important than ever to get better at understanding code, and to speed up that reading and understanding process. Now: if you want to get better at something, it’s helpful to set an aspirational goal for yourself. Something that’s above your current skill level, and drives you to get better and better every day. Here’s the goal I propose: we should be able to predict the behavior of every line of code, without running a single test. Let me say that again: We should be able to predict the behavior of every line of code, without running a single test
那么这让我们处于什么境地呢?如果 LLM 将为我们编写大量代码,那么我们面对的未来将是:人类花在编写代码上的时间会减少,而花在阅读和理解代码上的时间会增加,以确保代码通过我们的质量标准。这意味着,提高理解代码的能力,并加快阅读和理解的过程,比以往任何时候都更加重要。现在,如果你想在某方面有所进步,设定一个抱负目标会很有帮助。这个目标应高于你目前的技能水平,并推动你每天不断进步。我提出的目标是:我们应该能够预测每一行代码的行为,而无需运行任何测试。让我再说一遍:我们应该能够预测每一行代码的行为,而无需运行任何测试。
Now, I know you may be thinking: “Understand every line of code? Without testing? Are you serious?! There’s way too much complexity and legacy code for me to ever be able to do that!” You might think so. And if we take our hands off the wheel and relinquish all control, you might be right. But I’m here to tell you there’s a better way. We can design our code to be understandable. Consider that a decade or two ago, we used to think that writing multiple tests for every public method in an API would have been infeasible. It wasn’t because we didn’t want to, or that we weren’t trying. It was because we hadn’t designed our code to make testing possible at that level. But gradually, across the software industry, we changed our practices to design for testability. Today, designing our code for testability is so ingrained into our culture that doing anything else is practically unthinkable. We can do the same for understandability, so that our code enables us to understand it better.
我知道你可能在想:“理解每一行代码?不进行测试?你是认真的吗?!这里有太多的复杂性和遗留代码,我根本做不到!”你可能会这么想。如果我们撒手不管,放弃所有控制权,你可能是对的。但我在这里要告诉你,有更好的方法。我们可以将代码设计为可理解的。回想一二十年前,我们曾认为为 API 中的每个公共方法编写多个测试是不可行的。这并不是因为我们不想,也不是因为我们没有尝试,而是因为我们当时没有将代码设计为支持那种程度的测试。但逐渐地,整个软件行业改变了实践方式,转向为可测试性而设计。今天,为可测试性而设计代码已经深深植根于我们的文化中,以至于做其他任何事情几乎都是不可想象的。我们也可以对可理解性做同样的事情,让我们的代码能够让我们更好地理解它。
Our own Code Health guide makes a strong case for understandability. It says “For code to be sustainable, it must be understood. Change cannot be made unless a developer knows what the system currently provides, and what needs to change to adapt to new circumstances. As a result, key components of code health include good documentation, easy to understand error messages, and clear, simple code” Not only is designing for understandability important, but it’s a learnable skill. It’s not some magical ability that you either have or you don’t. There are specific, concrete actions you can take to improve the understandability of your code. I’m going to introduce you to a few of those today. Think of it as a “starter kit” for designing for understandability. We’ll also talk more about a general theme that runs through all of these practices, so that at a certain point you can find even more ways to design your code for understandability.
我们自己的《代码健康指南》有力地支持了可理解性。它指出:“代码要具有可持续性,就必须被理解。除非开发者知道系统当前提供了什么,以及需要做出什么改变来适应新情况,否则无法进行变更。因此,代码健康的关键组成部分包括良好的文档、易于理解的错误信息以及清晰简洁的代码。”为可理解性而设计不仅重要,而且是一项可习得的技能。它不是那种你非有即无的神奇能力。你可以采取具体、切实的操作来提高代码的可理解性。今天我将向大家介绍其中的几种。你可以将其视为为可理解性而设计的“入门套件”。我们还将讨论贯穿所有这些实践的一个通用主题,以便在某个阶段,你可以找到更多为可理解性而设计代码的方法。
The ideas I’m going to show you today are not new. But they’re not taught as well as they should be, when they’re taught at all, and most of us have only a rough understanding of how to apply them. My goal today is to sharpen that understanding just a little bit, and make it just a little more practical, so we can kickstart a process of making our code more understandable over time. So let’s get started on each of these Design for Understandability practices. The first one is Design by Contract. To motivate the idea, l
我今天展示的理念并不新鲜。但它们并没有得到应有的良好传授(如果确实有传授的话),而且我们大多数人对如何应用它们也只有粗略的了解。我今天的目标是稍微加深这种理解,并使其更具实践性,这样我们就可以启动一个过程,使我们的代码随着时间的推移变得更易于理解。那么,让我们开始探讨这些“为可理解性而设计”的实践吧。第一个是契约式设计。为了激发这个想法,我……