Skip to Content
先整理?
book

先整理?

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

第 12 章 提取助手 提取助手

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

你在例程中看到一个代码块,它的目的很明显,但与例程中其他代码的交互却很有限。将其提取为辅助例程。根据目的(而不是例程的工作方式)为例程命名。

有重构意识的人会认出这种整理中的 "提取方法"(Extract Method)。如果没有自动重构功能,执行这种整理/重构可能会很棘手。这就是为什么你应该在一个提供自动重构的环境中。毕竟,这已经是 21 世纪了。

我想提一下提取辅助程序的几种特殊情况。一种情况是当你需要在一个较大的例程中修改几行代码时,将这些代码提取为辅助程序,只修改辅助程序中的几行代码,然后,如果合理的话,将辅助程序内联回调用例程中。(通常你会发现自己越来越喜欢辅助程序,并将其保留下来)。所以,这样

routine()
    ...stuff that stays the same...
    ...stuff that needs to change...
    ...stuff that stays the same...

成为:

helper()
    ...stuff that needs to change...

routine()
    ...stuff that stays the same...
    helper()
    ...stuff that stays the same...

(如果你读过前面的内容,你就会知道这是连贯性,或者说是创造一个有凝聚力的元素。如果没有,别担心,我们会做到的)。

提取辅助程序的另一种情况是表达时间耦合(a() 需要在b() 之前调用)。如果您看到

foo.a()
foo.b()

经常,然后创建:

ab()
    a()
    b()

喜爱并不是保留助手的唯一原因。你经常会发现,在创建新助手数小时甚至数分钟后,你又想使用它了。界面是思考问题的工具。当我们准备好进行更抽象的思考,为我们的设计词汇表添加词汇时,新的界面就会出现。

不用担心在所有可能适用的地方使用辅助工具。可以在另一次 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