Figma内部:关于严格的空检查的案例研究

At Figma, we believe in continuously investing in product quality and developer productivity by looking at recurring types of bugs and addressing them in a systematic way. A few months ago, our engineering team completed a large effort that achieves both: turning on the strictNullChecks compiler flag for our front-end TypeScript codebase.

在 Figma,我们相信通过关注反复出现的错误类型并以系统的方式解决这些问题,来不断投资于产品质量和开发人员的生产力。几个月前,我们的工程团队完成了一项大型工作,实现了这两点:为我们的前端 TypeScript 代码库打开了strictNullChecks 编译器标志。

Code written with strict null checks operates under different type-checking rules and has a markedly different flavor than code written without. Converting our codebase to use it was a large undertaking that we think serves as an interesting case study in code migration.

用严格的空值检查编写的代码在不同的类型检查规则下运行,与没有编写的代码相比,有明显不同的味道。将我们的代码库转换为使用它是一项庞大的工程,我们认为它可以作为代码迁移的一个有趣案例。

In this blog post, we’d like to share how we performed this migration incrementally without slowing down concurrent product development.

在这篇博文中,我们想分享一下我们是如何在不拖累同时进行的产品开发的情况下逐步进行这种迁移的。

What are strict null checks?

什么是严格的无效检查?

In many popular typed programming languages (e.g. TypeScript, Java, etc), most or all variables are allowed to have the value null, indicating the absence of a more specific value.

在许多流行的类型化编程语言中(如TypeScript、Java等),大多数或所有的变量都允许有null ,表示没有一个更具体的值。


// strictNullChecks: off

interface Vector { x: number, y: number }

var v: Vector = { x: 1, y: 2 } // ✅ This is allowed
v = null // ✅ This is also allowed

function length(v: Vector) {
 // We may need to check for nullity when we're not sure.
 if (v) {
 return Math.sqrt(v.x * v.x + v.y * v.y)
 } else {
 // Return some default value?
 // What does it even mean to call length(null)?
 }
}

Most of us are used to this. However, this is not a fundamental property of programming languages. It was an intentional design choice by Tony Hoare back in 1965, who would later call it his billion dollar mistake.

我们大多数人都习惯于此。然而,这并不是编程语言的一个基本属性。这是托尼-霍尔(Tony Hoare)在1965年有意做出的设计选择,他后来称这是他的十亿美元的错误

And there are actually languages that do it diffe...

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

Главная - Вики-сайт
Copyright © 2011-2024 iteam. Current version is 2.139.0. UTC+08:00, 2024-12-27 20:16
浙ICP备14020137号-1 $Гость$