Skip to Content
先整理?
book

先整理?

by Kent Beck
May 2025
Intermediate to advanced
124 pages
1h 7m
Chinese
O'Reilly Media, Inc.
Content preview from 先整理?

第 3 章 对称性归一化

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

代码是有机生长的。有些人把 "有机 "当作贬义词。在我看来,这毫无意义。我们不可能一次性写完所有需要的代码。只有在我们从未学习过任何东西的情况下,。

在有机生长的过程中,同一个问题在不同的时间或不同的人那里可能会有不同的解决方法。这没有关系,但会给阅读带来困难。作为读者,你会喜欢一致性。如果你看到一个模式,你就可以自信地得出结论,知道发生了什么。

以懒惰地初始化变量为例。你可能会看到不同的写法:

foo()
    return foo if foo not nil
    foo := ...
    return foo

foo()
    if foo is nil
        foo := ...
    return foo

# tricky
foo()
    return foo not nil
        ? foo
        : foo := ...

# doubly tricky, assuming assignment is an expression
foo()
    return foo := foo not nil
        ? foo
        : ...

# even trickier, hiding the conditional
foo()
    return foo := foo || ...

(看看你能否找到或发明更多的变体)。

所有这些都是在说:"如果我们还没有计算和缓存 foo 的值"。它们各有利弊。作为读者,你会很快适应其中的任何一种。当两种或两种以上的模式交替使用时,情况就会变得混乱。作为读者,你希望差异意味着不同。在这里,差异掩盖了相同的事实。

选择一种方式。将其中一种变体转换成这种方式。每次 Tidy 一种不必要的变体形式--例如,首先是懒惰的初始化。

有时,共性会被额外的细节所掩盖。查找中相似但不相同的例程。将不同的部分从相同的部分中分离出来。

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

超越Vibe编程

超越Vibe编程

Addy Osmani
Go程序设计语言

Go程序设计语言

艾伦A. A.多诺万, 布莱恩W. 柯尼汉

Publisher Resources

ISBN: 9798341656949