本书是经典著作《重构》出版20年后的新版。书中清晰揭示了重构的过程,解释了重构的原理和最佳实践方式,并给出了何时以及何地应该开始挖掘代码以求改善。书中给出了60多个可行的重构,每个重构都介绍了一种经过验证的代码变换手法的动机和技术。本书提出的重构准则将帮助开发人员一次一小步地修改代码,从而减少了开发过程中的风险。 本书适合软件开发人员、项目管理人员等阅读,也可作为高等院校计算机及相关专业师生的参考读物。
AI Reading Assistant
Whole-book reading guide from stratified index samples; jump to passages in the text
AI guide
【One-Line Pitch】
A practical field manual for making working code easier to understand and change, one small, test-backed step at a time. Best for professional developers, tech leads, and architects who already write code and want a disciplined way to improve existing designs without breaking them.
【Book Arc】
- **Opening (~0%–12%)**: Sets up the core idea that refactoring is systematic, low-risk code transformation, then walks through a full worked example—a theater billing statement—showing how a tangled function is decomposed step by step.
- **Early (~12%–32%)**: Explains why refactoring matters (design decay, readability, future modification cost), introduces the catalog format, and covers the first practical moves: extracting functions, naming, and building a test harness before touching code.
- **Middle (~32%–52%)**: Moves into organizing data and behavior—encapsulating records and collections, replacing primitives with value objects, splitting phases, and moving functions—while emphasizing small commits and static checks.
- **Late (~52%–onward)**: Continues the catalog with more advanced transformations such as removing middlemen, dealing with inheritance, and handling repeated switches and large classes, always tied to code smells and safe sequencing.
- **Ending**: Reinforces the habit of continuous, incremental improvement and the social/practical side of refactoring—how to explain it to managers, how to pair on it, and how to keep tests trustworthy.
【Key Takeaways】
- **Refactoring is a controlled, stepwise discipline, not a rewrite** (Opening): each change is small, verified by tests, and committed frequently, which lowers the risk of altering working behavior.
- **Code smells are the trigger, not personal taste** (Early): repeated switches, large classes, long functions, and duplicated logic signal where to start; the catalog then offers a matching transformation.
- **Good names and small functions are the primary tools for readability** (Early): extracting a function and naming it well lets readers understand intent without reading the body.
- **Tests are the safety net that makes refactoring possible** (Early): the book stresses isolating business logic from UI and avoiding shared test fixtures, because flaky tests destroy confidence.
- **Data organization deserves as much attention as behavior** (Middle): encapsulating records and collections, introducing parameter objects, and replacing primitives with value objects prevent scattered, error-prone logic.
- **Refactoring improves design by removing duplication** (Early): less duplicated code means future changes touch one place, reducing the chance of missing a related edit.
- **Refactoring supports code review and collaboration** (Early): reviewing code while refactoring—ideally with the author present—produces concrete, immediately applicable improvements.
- **The catalog is a reference, not a novel** (Opening): read the first four chapters fully, then consult individual refactorings only when you need them.
【Reading Tips】
- Read Chapters 1–4 carefully; they give the mindset, the smell vocabulary, and the test-first workflow that make the rest usable.
- Treat the refactoring catalog as a lookup reference: skim the names once, then return to a specific entry when you actually face that problem.
- Work through the JavaScript examples actively—especially the billing and province/production examples—to see how small steps accumulate.
- Pay attention to the “how to talk to your manager” and code-review sections; they address the organizational friction that often blocks refactoring.
- Keep a personal list of smells you recognize in your own codebase and match them to catalog entries as you go.
【Coverage Limits】
The excerpts cover the book’s framing, motivation, testing setup, and a representative slice of the catalog (extract function, inline, split phase, encapsulate record/collection, value objects, remove middle man). They do not cover the full 60+ refactorings, later inheritance-focused chapters, or the complete code smell taxonomy in detail.
Page 18
法,保证每次只走 一步。最后,我对这种实践方式的效果感到十分惊讶。我不但对产生的代码更有 信心,而且开发压力也小了很多。因此,我极力推荐你试试这些重构手法,你和 你的程序都将因此更美好。 ——Erich Gamma Object Technology International, Inc. 谁该阅读本书 本书的目...
View in text
Excerpt 2
数剧类在计算积分时都会检查观众数是否达到30, 仅一小部分品类有所不同。因此,将更为通用的逻辑放到超类作为默认条件,出 现特殊场景时按需覆盖它,听起来十分合理。于是我将一部分喜剧的逻辑下移到 子类。 class PerformanceCalculator... get volumeCredits() { retu...
View in text
Excerpt 3
ducer),另一 个用来描述一个行省(Province)。Province类的构造函数接收一个JavaScript对 象,这个对象的内容我们可以想象是由一个JSON文件提供的。 惑,因此我要试着将它们提到一处公共的地方,以此来消灭重复。一种方案就是 把常量提取到外层作用域里。 describe('provinc...
View in text
Excerpt 4
ation { constructor(data) { this._data = data; } } 顶层作用域 const organization = new Organization({name: "Acme Gooseberries", country: "GB"}); function getRawDa...
View in text
Excerpt 5
retreiveOrder(); 3 const baseCharge = pricingPlan.base; 8.8 以管道取代循环(Replace Loop with Pipeline) const names = []; for (const i of input) { if (i.job === "pro...
View in text
Excerpt 6
aRoom.daysTempRange.high; if (!aPlan.xxNEWwithinRange(aRoom.daysTempRange)) alerts.push("room temperature went outside range"); 在修改调用处时,我可能会发现一些代码在修改后已经不再需要,...
View in text
Excerpt 7
hlyCost = monthlyCost; 但继承也有其短板。最明显的是,继承这张牌只能打一次。导致行为不同的 原因可能有多种,但继承只能用于处理一个方向上的变化。比如说,我可能希 望“人”的行为根据“年龄段”不同,并且根据“收入水平”不同。使用继承的话,子 类可以是“年轻人”和“老人”,也可以是“富人”和“穷...
View in text
Excerpt 8
数下移(359),字段下移(361),以委托取代子类(381),以委托取代超类(399) 遗赠 Repeated 重复 Switches 的 79 以多态取代条件表达式(272) switch 霰弹 Shotgun Surgery 搬移函数(198),搬移字段(207),函数组合成类(144),函数组合成变换(1...
View in text
Tags
AI categories
SoftwareProgrammingCode
Text Preview (First 20 pages)
Registered users can read the full content for free
Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.
Generating text preview…
Loading comments...
Reply to Comment
Edit Comment