斯特鲁斯特鲁普法则 (2024)
Stroustrup's Rule (2024)

原始链接: https://buttondown.com/hillelwayne/archive/stroustrups-rule/

在这篇关于“斯特劳斯特鲁普法则”(Stroustrup’s Rule)的思考中,作者指出,随着编程语言社区的成熟,语言设计往往会从显式、冗长的语法演变为简洁、优雅的符号。 最初,程序员需要显式的语法来构建心理模型,并充分理解功能运作的原理。随着时间推移,同样的语法会变成“视觉噪音”。专家们为了效率更倾向于简洁,这促使了 Rust 的 `?` 运算符或 Python 的“海象运算符”(walrus operator)等特性的引入。 然而,这种转变引发了矛盾:满足资深开发者的特性可能会疏远初学者,使得语言变得愈发难以学习。作者指出,这一法则同样影响教学;虽然专家偏爱简写方法(例如在 TLA+ 中使用函数集),但初学者从显式的、循序渐进的逻辑中获益更多。归根结底,语言向专家友好的简洁性演变,往往会在无意中提高了新手的入门门槛。 *(注:作者目前正在提供“程序员的妥当逻辑”(Hanuka Sale Logic for Programmers)课程 40% 的折扣,活动持续至 1 月 2 日。)*

Hacker News 最新 | 往日 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 斯特劳斯鲁普法则 (2024) (buttondown.com/hillelwayne) 7 点,作者:bmacho,1 小时前 | 隐藏 | 往日 | 收藏 | 讨论 | 帮助 准则 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文

Just finished two weeks of workshops and am exhausted, so this one will be light.

Hanuka Sale

Logic for Programmers is on sale until the end of Chanukah! That's Jan 2nd if you're not Jewish. Get it for 40% off here.

I first encountered Stroustrup's Rule on this defunct webpage:

One of my favorite insights about syntax design appeared in a retrospective on C++ by Bjarne Stroustrup:

  • For new features, people insist on LOUD explicit syntax.
  • For established features, people want terse notation.

The blogger gives the example of option types in Rust. Originally, the idea of using option types to store errors was new for programmers, so the syntax for passing an error was very explicit:

let file = match File::open("file.txt") {
    Ok(file) => file,
    Err(err) => { return err; }
}

Once people were more familiar with it, Rust added the try! macro to reduce boilerplate, and finally the ? operator to streamline error handling further.

I see this as a special case of mental model development: when a feature is new to you, you don't have an internal mental model so need all of the explicit information you can get. Once you're familiar with it, explicit syntax is visual clutter and hinders how quickly you can parse out information.

(One example I like: which is more explicit, user_id or user_identifier? Which do experienced programmers prefer?)

What's interesting is that it's often the same people on both sides of the spectrum. Beginners need explicit syntax, and as they become experts, they prefer terse syntax.

The rule applies to the overall community, too. At the beginning of a language's life, everybody's a beginner. Over time the ratio of experts to beginners changes, and this leads to more focus on "expert-friendly" features, like terser syntax.

This can make it harder for beginners to learn the language. There was a lot of drama in Python over the "walrus" assignment operator:

# Without walrus
val = dict.get(key) # `None` if key absent
if val:
    print(val)


# With walrus
if val := dict.get(key):
    print(val)

Experts supported it because it made code more elegant, teachers and beginners opposed it because it made the language harder to learn. Explicit syntax vs terse notation.

Does this lead to languages bloating over time?

In Teaching

I find that when I teach language workshops I have to actively work against Stroustrup's Rule. The terse notation that easiest for me to read is bad for beginners, who need the explicit syntax that I find grating.

One good example is type invariants in TLA+. Say you have a set of workers, and each worker has a counter. Here's two ways to say that every worker's counter is a non-negative integer:

\* Bad
\A w \in Workers: counter[w] >= 0

\* Good
counter \in [Workers -> Nat]

The first way literally tests that for every worker, counter[w] is non-negative. The second way tests that the counter mapping as a whole is an element of the appropriate "function set"— all functions between workers and natural numbers.

The function set approach is terser, more elegant, and preferred by TLA+ experts. But I teach the "bad" way because it makes more sense to beginners.

联系我们 contact @ memedata.com