``` 格式化代码是不必要的(我们早在80年代就知道了) ```
Formatting code should be unnecessary

原始链接: https://maxleiter.com/blog/formatting

一位高中计算机科学老师揭示了一种来自 20 世纪 80 年代令人惊讶的先进编码方法,解决了现代工具的痛点。Ada 编程语言(用于关键系统,如国际空间站和 F-22 战斗机)没有将源代码存储为文本,而是在 Rational R1000 等机器上使用一种名为 DIANA 的中间表示 (IR)。 该系统完全绕过了格式化争论。DIANA 本身不能直接被人阅读;编辑器会根据用户的偏好将其“美化”打印出来。R1000 还具有增量编译、语义分析和内置调试等功能——这些功能在早期系统中普遍缺失。 作者遗憾地指出,尽管其他领域取得了进步,但现代开发仍然面临格式化和代码检查问题,本质上是“倒退”回了这种更早、更灵活的方法。虽然完全采用 80 年代的技术不是答案,但作者建议探索超越严格格式化规则并采用更具适应性的代码表示的解决方案。

## Hacker News 讨论总结:格式化与代码呈现 一场 Hacker News 讨论围绕着将代码存储为抽象语法树 (AST) 或中间表示 (IR),而不是纯文本,并让编辑器投射个性化格式化的想法展开。核心争论在于,个性化格式化的好处是否超过了纯文本源代码文件所提供的简单性和可组合性。 许多评论者反对这个想法,认为会破坏现有的 Unix 工具,如 `grep`、`diff` 和版本控制。保持兼容性需要复杂的工具来在 IR 和可读文本之间进行转换。另一些人指出 `gofmt` 和 `black`(Python 格式化工具)等成功案例,它们强制执行标准风格,消除了格式化争论。 一些人支持这个概念,认为通用的中间编码仍然可以允许基于文本的工具发挥作用,或者 IDE 可以无缝处理格式化转换。讨论涉及开发者偏好、代码可读性和可维护性之间的权衡,一个反复出现的主题是,强制执行一致的风格通常比允许个性化格式化更实用。最终,这场辩论凸显了灵活性与普遍可访问的、基于文本的代码库的优势之间的紧张关系。
相关文章

原文
and we knew this back in the 80s

I had a (maybe slightly overqualified) computer science teacher back in highschool, Mr. Paige. He worked on the Ada compiler and has been programming since the early 80s.

One day I complained about linter tooling that was driving me nuts. I said something to the effect of, "it's 2016, how are we still dealing with this sort of thing?"

Turns out, that problem was solved four decades ago (well, three at that point). Back when he was working on Ada, they didn't store text sources at all — they used an IR called DIANA. Everyone had their own pretty-printing settings for viewing it however they wanted.

We've been debating some linter settings at work recently and I keep thinking back to Mr. Paige. It's 2025, how are we still dealing with this sort of thing?


Well, to answer that it would help to know what we're missing.

I believe he was working with the Rational R1000, of which there isn't a ton of info (like all things Ada, it was used by the DoD):

The R1000 had a lot of bleeding-edge features: incremental compilation, semantic analysis, version control, and first-class debugging all built-in. It was a workstation similar to the Xerox Alto but using Ada instead of Smalltalk.

DIANA (Descriptive Intermediate Attributed Notation for Ada) was a key component of Ada that enabled a lot of the more advanced features.

A diagram scanned from a type-written page. It shows a diagram of Ada Source => Syntax & Semantics => DIANA => Normalization phase => Middle End => simple IR optimization phase => IR => Code Gen. => Executable Image

Taken from Experiences with Code Generation (1984)

Instead of storing plain-text source code, the R1000 wrote DIANA. The compiler and the IDE built into the machine both understood DIANA too, so you could view the source however you wanted. Spaces vs. tabs didn't matter because neither affects the semantics and the editor on the system let you modify the program tree directly (known today as projectional editing).

Grady Booch summarizes it well:

R1000 was effectively a DIANA machine. We didn't store source code: source code was simply a pretty-printing of the DIANA tree.

Imagine that. No wasted time due to formatting discussions or fighting linters, without forcing everyone into the same editor setup (looking at you, eslint-config-airbnb).

And there were other benefits:

Using DIANA with hardware acceleration made it possible to do incremental compilation (unheard of at the time, for strongly typed languages), easy refactoring (though that word had not yet been invented), and incredibly fast integration (essential for the large systems that we being built with Ada).

Today, we don't need to worry about hardware-accelerated compilation (hopefully), and we have better tools for refactoring (thanks, Claude). But with formatting, we regressed. I'm not advocating for everyone to use projectional editing and a live environment (although I think they're awesome and we should be exploring them more), but surely we can figure out something that fits into todays programming paradigms.

Further reading

This post was meant to be me saying "it'd be easier if we just pushed minified code", but I had too much fun researching the R1000 during it. Here are some of the documents I looked at:

联系我们 contact @ memedata.com