Skip to Content
先整理?
book

先整理?

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

第 13 章 一堆

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

有时,你读到的代码被分割成许多小块,但却妨碍了你对它的理解。根据需要尽可能多地内联代码,直到所有代码都集中在一起。 在此基础上进行 Tidy。

代码的最大成本是阅读和理解代码的成本,而不是编写代码的成本。Tidy 首先偏向于使用大量的小片段,理论上是为了增加内聚性,以此来减少耦合;实际上是为了减少在任何时候都需要在头脑中掌握的细节量。

这种偏向于小片段的目标是使代码能够一次理解一点。但有时,这个过程会出错。由于小片段之间的相互作用,代码变得更加难以理解。为了恢复代码的清晰度,首先必须将代码捋顺,然后才能提取出新的、更容易理解的部分。

您要寻找的一些症状是

  • 冗长、重复的参数列表

  • 重复代码,尤其是重复条件

  • 辅助例程命名不当

  • 共享可变数据结构

由于偏向于更多、更小的作品,在整理时堆成一堆感觉很奇怪。然而,这却让我感到莫名的满足。我一直在尝试理解零散的代码。我开始怀疑自己的能力。我转了 180 度,开始将所有代码集中在一起(自动重构真的很有帮助,但如果有必要,我也会手动重构)。真是如释重负!

随着这堆东西越积越多,我的脑海中开始浮现出它的形状。我明白了--我们先计算这个,然后用它来计算那个!他们为什么不直说呢?现在,我可以提出标题问题:我应该先整理呢?还是只做我现在看到的改动?

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