重构:改善既有代码的设计(第2版) ([美]马丁·福勒(Martin Fowler))(Z-Library)
Code
本书是经典著作《重构》出版20年后的更新版。书中清晰揭示了重构的过程,解释了重构的原理和实践方式,并给出了何时以及何地应该开始挖掘代码以求改善。书中给出了60多个可行的重构,每个重构都介绍了一种经过验证的代码变换手法的动机和技术。本书提出的重构准则将帮助开发人员一次一小步地修改代码,从而减少了开发过程中的风险。 本书适合软件开发人员、项目管理人员等阅读,也可作为高等院校计算机及相关专业师生的参考读物。
211
Views
0
Downloads
0.00
Total Donations
AI Guide
AI Reading Assistant
Whole-book reading guide from stratified index samples; jump to passages in the text
AI guide
# 【One-Line Pitch】
The definitive 20th-anniversary guide to safely improving existing code through small, behavior-preserving steps—essential reading for professional developers who maintain or extend real-world systems.
# 【Book Arc】
- **Opening (~0%–10%)**: Front matter, endorsements, and translator's preface establish why refactoring matters—code decays over time, and the only cure is continuous, disciplined improvement. The translator's preface highlights how the 2nd edition deliberately focuses on smaller, more granular techniques rather than large-scale rewrites.
- **Early (~10%–23%)**: Fowler opens with a worked example (a theater billing program) to demonstrate refactoring in action before explaining principles. Chapter 1 walks through decomposing a long function, extracting logic, and preparing for new features—showing why structure matters when requirements change.
- **Early (~23%–32%)**: The book's philosophy crystallizes: refactoring means improving design after code is written, not before. Fowler explains the "two hats" (adding features vs. refactoring), when to refactor, and why reliable self-checking tests are the non-negotiable foundation.
- **Middle (~32%–48%)**: The core catalog begins. Chapters 2–4 cover principles, code smells (with Kent Beck's contributions), and building a test suite. Chapter 5 onward presents the refactoring catalog itself, organized by purpose—starting with composing methods like Extract Function and Inline Function.
- **Middle (~48%–52%+)**: The catalog continues through encapsulation, moving features between objects, reorganizing data, simplifying conditionals, refactoring APIs, and handling inheritance. Each entry follows a consistent format: motivation, mechanics, and examples.
# 【Key Takeaways】
- **Refactoring is behavior-preserving improvement** (Early): The definition is precise—modify code without changing external behavior to improve internal structure. This distinguishes refactoring from rewriting or feature addition, and it's the foundation for everything else in the book.
- **Tests are the safety net that makes refactoring possible** (Early): Before any refactoring, you need a reliable, self-checking test suite. Fowler treats tests as bug detectors that protect against human error—writing expectations twice means making the same mistake twice to fool the system.
- **Code smells signal where to refactor** (Early): Kent Beck's concept of "bad smells" (Duplicated Code, Long Function, Feature Envy, etc.) gives developers a shared vocabulary for identifying problems. The 2nd edition catalogs over 20 distinct smells, each with suggested remedies.
- **Small steps compound into significant design improvements** (Middle): Refactoring isn't dramatic rewriting—it's a series of tiny, safe transformations like Extract Function or Rename Variable. Each step is simple enough to verify, but accumulated changes can fundamentally improve design quality.
- **Variable naming is a core refactoring skill** (Middle): Fowler demonstrates renaming variables (e.g., `thisAmount` to `result`) and adding type prefixes to parameters in dynamic languages. Clear naming is what makes code readable by humans, not just compilers.
- **Refactoring is driven by upcoming changes, not aesthetics** (Middle): The theater example shows refactoring becomes necessary when you need to add features (HTML output, new play types). If code works and won't change, refactoring may be unnecessary—it's future modification that justifies the effort.
- **The catalog format makes refactoring a learnable discipline** (Middle): Each technique follows a consistent structure (motivation, mechanics, examples), turning what feels like intuition into a repeatable process. The 2nd edition adds finer-grained techniques like Split Loop and Slide Statements that address real legacy-code challenges.
# 【Reading Tips】
- **Read Chapter 1 first, even if you're tempted to skip ahead**: The worked example (theater billing) demonstrates the entire refactoring mindset—test first, extract functions, rename variables, and prepare for change. It makes abstract principles concrete.
- **Skim the endorsements and preface, but don't skip the translator's preface**: It explains what changed between editions (removal of "large refactorings," addition of finer-grained techniques) and frames the book's philosophy of "small steps" versus "wholesale rewriting."
- **Use the catalog (Chapters 5–12) as a reference, not a cover-to-cover read**: Once you understand the principles and smells, dip into specific techniques when you encounter problems. Each entry is self-contained with motivation and mechanics.
- **Pay special attention to the test-building chapter (Chapter 4)**: Many developers skip testing, but Fowler argues it's the prerequisite for all refactoring. Understanding self-checking tests and boundary conditions will make every other technique safer.
- **Watch for the "two hats" distinction throughout**: Fowler repeatedly emphasizes separating "adding features" from "refactoring." Mixing these modes is a common source of errors—internalize this mental discipline early.
# 【Coverage Limits】
Excerpts cover the book's front matter, philosophy, and the beginning of the worked example in Chapter 1, plus the table of contents for the full catalog. Specific mechanics for most individual refactorings (Chapters 6–12) are not detailed in the source material.
#
Passage locations
Excerpt 1
itive with Object) 7.4 以查询取代临时变量(Replace Temp with Query) 7.5 提炼类(Extract Class) 7.6 内联类(Inline Class) 7.7 隐藏委托关系(Hide Delegate) 7.8 移除中间人(Remove Middle Man)...
View in text
Excerpt 2
元素似乎是浪费:为何不直接一步到位改变到完善的结果状态呢?然而这些临时元素所代表的,是对变更过程(而非只是结果)的设计。缺乏对过程的精心设计与必要投入,只抱着对结果的美好憧憬提刀上阵,遇到困难就靠“奋斗精神”和加班解决,这种“刀劈斧砍”不止发生在缺乏审慎的“重构”现场,又何尝不是我们这个行业的缩影? 是以,重构这...
View in text
Excerpt 3
itry Kirsanov负责文字编辑工作;Alina Kirsanova负责排版和制作索引。我也很高兴与他们合作。 服务与支持 本书由异步社区出品,社区(https://www.epubit.com/)为您提供相关资源和后续服务。 提交勘误 作者和编辑尽最大努力来确保书中内容的准确性,但难免会存在疏漏。欢迎您将...
View in text
Excerpt 4
- 30); } break; case "comedy": thisAmount = 30000; if (perf.audience > 20) { thisAmount += 10000 + 500 * (perf.audience - 20); } thisAmount += 300 * perf.aud...
View in text
Recommended for You
{{#thumbnailUrl}}
{{/thumbnailUrl}}
{{^thumbnailUrl}}
{{/thumbnailUrl}}
Loading recommended books...
Failed to load, please try again later
Tip the Site
Scan the WeChat Pay or Alipay code to tip. No login required.
WeChat Pay
Alipay