self-documenting-code

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese
Code should be self documenting
How you split logic into functions and shape the data they pass around determines how well a codebase holds up over time.
Semantic Functions
Semantic functions are the building blocks of any codebase, a good semantic function should be as minimal as possible in order to prioritize correctness in it. A semantic function should take in all required inputs to complete its goal and return all necessary outputs directly. Semantic functions can wrap other semantic functions to describe desired flows and usage; as the building blocks of the codebase, if there are complex flows used everywhere that are well defined, use a semantic function to codify them.
Side effects are generally undesirable in semantic functions unless they are the explicit goal because semantic functions should be safe to re-use without understanding their internals for what they say they do. If logic is complicated and it's not clear what it does in a large flow, a good pattern is to break that flow up into a series of self describing semantic functions that take in what they need, return the data necessary for the next step, and don't do anything else. Examples of good semantic functions range from quadratic_formula() to retry_with_exponential_backoff_and_run_y_in_between<Y: func, X: Func>(x: X, y: Y). Even if these functions are never used again, future humans and agents going over the code will appreciate the indexing of information.
Semantic functions should not need any comments around them, the code itself should be a self describing definition of what it does. Semantic functions should ideally be extremely unit testable because a good semantic function is a well defined one.
Pragmatic Functions
Pragmatic functions should be used as wrappers around a series of semantic functions and unique logic. They are the complex processes of your codebase. When making production systems it's natural for the logic to get messy, pragmatic functions are the organization for these. These should generally not be used in more than a few places, if they are, consider breaking down the explicit logic and moving it into semantic functions. For example provision_new_workspace_for_github_repo(repo, user) or handle_user_signup_webhook(). Testing pragmatic functions falls into the realm of integration testing, and is often done within the context of testing whole app functionality. Pragmatic functions are expected to change completely over time, from their insides to what they do. To help with that, it's good to have doc comments above them. Avoid restating the function name or obvious traits about it, instead note unexpected things like "fails early on balance less than 10", or combatting other misconceptions coming from the function name. As a reader of doc comments take them with a grain of salt, coders working inside the function may have forgotten to update them, and it's good to fact check them when you think they might be incorrect.
Models
The shape of your data should make wrong states impossible. If a model allows a combination of fields that should never exist together in practice, the model isn't doing its job. Every optional field is a question the rest of the codebase has to answer every time it touches that data, and every loosely typed field is an invitation for callers to pass something that looks right but isn't. When models enforce correctness, bugs surface at the point of construction rather than deep inside some unrelated flow where the assumptions finally collapse. A model's name should be precise enough that you can look at any field and know whether it belongs — if the name doesn't tell you, the model is trying to be too many things. When two concepts are often needed together but are independent, compose them rather than merging them — e.g. UserAndWorkspace { user: User, workspace: Workspace } keeps both models intact instead of flattening workspace fields into the user. Good names like UnverifiedEmail, PendingInvite, and BillingAddress tell you exactly what fields belong. If you see a phone_number field on BillingAddress, you know something went wrong.
Values with identical shapes can represent completely different domain concepts: { id: "123" } might be a DocumentReference in one place and a MessagePointer in another, and if your functions just accept { id: String }, the code will accept either one without complaint. Brand types solve this by wrapping a primitive in a distinct type so the compiler treats them as separate: DocumentId(UUID) instead of a bare UUID. With branding in place, accidentally swapping two IDs becomes a syntax error instead of a silent bug that surfaces three layers deep.
Where Things Break
Breaks commonly happen when a semantic function morphs into a pragmatic function for ease, and then other places in the codebase that rely on it end up doing things they didn't intend. To solve this, be explicit when creating a function by naming it instead of by what it does, but by where it's used. The nature of their names should make it clear to other programmers in their names that their behavior is not tightly defined and should not be relied on for the internals to do an exact task, and make debugging regressions from them easier.
Models break the same way but slower. They start focused, then someone adds "just one more" optional field because it's easier than creating a new model, and then someone else does the same, and eventually the model is a loose bag of half-related data where every consumer has to guess which fields are actually set and why. The name stops describing what the data is, the fields stop cohering around a single concept, and every new feature that touches the model has to navigate states it was never designed to represent. When a model's fields no longer cohere around its name, that's the signal to split it into the distinct things it's been coupling together.
代码应当具备自文档化特性
你如何将逻辑拆分为函数,以及如何设计函数间传递的数据结构,决定了代码库长期的可维护性。
Semantic函数
Semantic函数是任何代码库的构建模块,一个优秀的Semantic函数应尽可能精简,以优先保证正确性。Semantic函数应接收完成目标所需的全部输入,并直接返回所有必要的输出。Semantic函数可以嵌套其他Semantic函数,以此描述预期的流程和用法;作为代码库的基础组件,如果存在一些在各处都被使用的、定义清晰的复杂流程,可以用Semantic函数将其规范化。
除非副作用是函数的明确目标,否则Semantic函数中通常应避免副作用,因为Semantic函数应当能够安全复用,使用者无需理解其内部实现,只需关注其对外声明的功能。如果在一个大型流程中,某段逻辑复杂且功能不明确,一个好的做法是将该流程拆分为一系列具备自描述性的Semantic函数——每个函数仅接收自身所需的输入,返回下一步所需的数据,不执行其他无关操作。优秀的Semantic函数示例包括quadratic_formula(),以及retry_with_exponential_backoff_and_run_y_in_between<Y: func, X: Func>(x: X, y: Y)。即使这些函数不会被再次使用,未来查看代码的开发人员和Agent也会感谢这种信息索引方式。
Semantic函数不需要额外的注释,代码本身就应能自描述其功能。理想情况下,Semantic函数应具备极强的单元可测试性,因为优秀的Semantic函数必然是定义清晰的。
Pragmatic函数
Pragmatic函数应作为一系列Semantic函数和独特逻辑的包装器,它们是代码库中的复杂处理流程。在构建生产系统时,逻辑变得混乱是很自然的事,Pragmatic函数就是用来组织这些复杂逻辑的。这类函数通常不应在多个地方使用,如果出现这种情况,应考虑将其中的明确逻辑拆分出来,转移到Semantic函数中。例如provision_new_workspace_for_github_repo(repo, user)或handle_user_signup_webhook()。对Pragmatic函数的测试属于集成测试范畴,通常在测试整个应用功能的场景下进行。Pragmatic函数的内部实现和功能会随时间发生彻底变化,因此在函数上方添加文档注释是个好做法。注释应避免重复函数名或显而易见的特性,而是记录一些意外行为,比如“当余额小于10时提前失败”,或者纠正由函数名引发的其他误解。阅读文档注释时应保持谨慎,编写函数的开发人员可能忘记更新注释,当你认为注释可能不正确时,最好进行事实核查。
数据模型
数据模型的设计应杜绝非法状态的存在。如果一个模型允许在实际场景中永远不会共存的字段组合,那么这个模型的设计是失败的。每个可选字段都会让代码库中所有接触该数据的地方不得不判断其是否存在,而每个弱类型字段都会导致调用者传入看似正确但实际不符合要求的值。当模型能够强制保证正确性时,bug会在数据构建阶段就暴露出来,而不是在某个不相关的流程深处,当假设不成立时才突然出现。模型的名称应足够精准,让你看到任意字段就能判断它是否属于该模型——如果名称无法做到这一点,说明该模型试图承载过多职责。当两个概念经常一起使用但彼此独立时,应采用组合而非合并的方式——例如UserAndWorkspace { user: User, workspace: Workspace }可以保留两个模型的完整性,而不是将workspace的字段扁平合并到user中。像UnverifiedEmail、PendingInvite和BillingAddress这类优秀的名称,能清晰告诉你哪些字段属于该模型。如果你在BillingAddress中看到phone_number字段,就知道哪里出问题了。
结构相同的值可能代表完全不同的领域概念:{ id: "123" }在某个场景下可能是DocumentReference,在另一个场景下可能是MessagePointer,如果你的函数仅接收{ id: String }类型,代码会无差别地接受这两种值。品牌类型(Brand types)通过将原始类型包装为不同的类型来解决这个问题,这样编译器会将它们视为不同的类型:比如使用DocumentId(UUID)而非裸UUID。有了品牌类型的约束,意外交换两个ID会变成语法错误,而不是在三层调用之后才暴露的隐性bug。
问题出现的场景
常见的问题场景是,为了图方便,Semantic函数逐渐演变成Pragmatic函数,而代码库中依赖该函数的其他部分最终会出现非预期的行为。解决这个问题的方法是,在创建函数时,不要根据它的功能命名,而是根据它的使用场景命名。这样的命名方式能让其他开发人员清楚地知道,该函数的行为没有严格定义,不应依赖其内部实现来完成精确任务,同时也能更轻松地调试由它引发的回归问题。
数据模型出现问题的过程类似,但更缓慢。它们最初是专注的,然后有人因为创建新模型太麻烦,添加了“再多一个”可选字段,接着其他人也效仿,最终模型变成了一个装满半相关数据的松散集合,每个使用者都必须猜测哪些字段是实际被设置的,以及为什么被设置。模型名称不再能描述数据的本质,字段不再围绕单一概念聚合,每个接触该模型的新功能都必须处理它从未被设计过的状态。当模型的字段不再与其名称相符时,就应该将它拆分为多个独立的模型,拆分出之前被耦合在一起的不同概念。