logo资料库

Working.Effectively.with.Legacy.Code.pdf

第1页 / 共328页
第2页 / 共328页
第3页 / 共328页
第4页 / 共328页
第5页 / 共328页
第6页 / 共328页
第7页 / 共328页
第8页 / 共328页
资料共328页,剩余部分请下载后查看
Table of Contents
Part I: The Mechanics of Change
Chapter 1. Changing Software
Four Reasons to Change Software
Improving Design
Risky Change
Chapter 2. Working with Feedback
Software Vise
What Is Unit Testing?
Test Harnesses
Higher-Level Testing
Test Coverings
The Legacy Code Dilemma
Figure 2.2. Invoice update classes with dependencies broken.
The Legacy Code Change Algorithm
Chapter 3. Sensing and Separation
Faking Collaborators
Fake Objects Support Real Tests
The Two Sides of a Fake Object
Chapter 4. The Seam Model
A Huge Sheet of Text
Seams
Seam
Seam Types
Seam
Enabling Point
Link Seams
Usage Tip
Object Seams
Chapter 5. Tools
Automated Refactoring Tools
Tests and Automated Refactoring
Mock Objects
Unit-Testing Harnesses
General Test Harnesses
Part II: Changing Software
Chapter 6. I Don't Have Much Time and I Have to Change It
It Happens Someplace Every Day
Sprout Method
Sprout Class
Wrap Method
Wrap Class
The Decorator Pattern
Summary
Chapter 7. It Takes Forever to Make a Change
Understanding
Lag Time
Breaking Dependencies
The Dependency Inversion Principle
Figure 7.5. Package structure.
Summary
Chapter 8. How Do I Add a Feature?
Test-Driven Development (TDD)
Remove Duplication
TDD and Legacy Code
Programming by Difference
The Liskov Substitution Principle
Figure 8.7. Normalized hierarchy.
Summary
Chapter 9. I Can't Get This Class into a Test Harness
The Case of the Irritating Parameter
Figure 9.1. RGHConnection.
Test Code vs. Production Code
Pass Null
Null Object Pattern
The Case of the Hidden Dependency
The Case of the Construction Blob
The Case of the Irritating Global Dependency
The Case of the Horrible Include Dependencies
The Case of the Onion Parameter
The Case of the Aliased Parameter
Chapter 10. I Can't Run This Method in a Test Harness
The Case of the Hidden Method
Subverting Access Protection
The Case of the "Helpful" Language Feature
The Case of the Undetectable Side Effect
Command/Query Separation
Figure 10.1. AccountDetailFrame.
Chapter 11. I Need to Make a Change. What Methods Should I Test?
Reasoning About Effects
IDE Support for Effect Analysis
Figure 11.1. declarations impacts geTDeclarationCount.
Reasoning Forward
Figure 11.9. Effects through the Element class.
Effect Propagation
Tools for Effect Reasoning
Learning from Effect Analysis
Simplifying Effect Sketches
Effects and Encapsulation
Chapter 12. I Need to Make Many Changes in One Area. Do I Have to Break Dependencies for All the Classes Involved?
Interception Points
Higher-Level Interception Points
Pinch Point
Judging Design with Pinch Points
Using Effect Sketches to Find Hidden Classes
Pinch Point Traps
Chapter 13. I Need to Make a Change, but I Don't Know What Tests to Write
Characterization Tests
The Method Use Rule
Characterizing Classes
When You Find Bugs
Targeted Testing
Refactoring Tool Quirks
A Heuristic for Writing Characterization Tests
Chapter 14. Dependencies on Libraries Are Killing Me
Chapter 15. My Application Is All API Calls
Figure 15.1. A better mailing list server.
Chapter 16. I Don't Understand the Code Well Enough to Change It
Notes/Sketching
Listing Markup
Scratch Refactoring
Delete Unused Code
Chapter 17. My Application Has No Structure
Telling the Story of the System
Naked CRC
Conversation Scrutiny
Chapter 18. My Test Code Is in the Way
Class Naming Conventions
Test Location
Chapter 19. My Project Is Not Object Oriented. How Do I Make Safe Changes?
An Easy Case
A Hard Case
Adding New Behavior
Taking Advantage of Object Orientation
It's All Object Oriented
Chapter 20. This Class Is Too Big and I Don't Want It to Get Any Bigger
Single-Responsibility Principle (SRP)
Figure 20.1. Rule parser.
Seeing Responsibilities
Heuristic #1: Group Methods
Heuristic #2: Look at Hidden Methods
Figure 20.3. RuleParser and TermTokenizer.
Heuristic #3: Look for Decisions That Can Change
Heuristic #4: Look for Internal Relationships
Figure 20.4. Variables in the Reservation class.
Figure 20.6. Feature sketch for Reservation.
Heuristic #5: Look for the Primary Responsibility
Figure 20.11. The ScheduledJob class.
Interface Segregation Principle (ISP)
Figure 20.14. Segregating the interface of ScheduledJob.
Heuristic #6: When All Else Fails, Do Some Scratch Refactoring
Heuristic #7: Focus on the Current Work
Other Techniques
Moving Forward
After Extract Class
Chapter 21. I'm Changing the Same Code All Over the Place
Figure 21.1. AddEmployeeCmd and LoginCommand.
First Steps
Deciding Where to Start
Figure 21.2. Command hierarchy.
Figure 21.3. Pulling up writeField.
Abbreviations
Open/Closed Principle
Chapter 22. I Need to Change a Monster Method and I Can't Write Tests for It
Varieties of Monsters
Tackling Monsters with Automated Refactoring Support
Figure 22.4. Logic class extracted from CommoditySelectionPanel.
The Manual Refactoring Challenge
Strategy
Chapter 23. How Do I Know That I'm Not Breaking Anything?
Hyperaware Editing
Single-Goal Editing
Preserve Signatures
Lean on the Compiler
Chapter 24. We Feel Overwhelmed. It Isn't Going to Get Any Better
Part III: Dependency-Breaking Techniques
Chapter 25. Dependency-Breaking Techniques
Adapt Parameter
Steps
Break Out Method Object
Steps
Definition Completion
Encapsulate Global References
Steps
Expose Static Method
Steps
Extract and Override Call
Extract and Override Factory Method
Steps
Extract and Override Getter
Steps
Extract Implementer
Steps
Extract Interface
Interface Naming
Steps
Extract Interface and Non-Virtual Functions
Introduce Instance Delegator
Introduce Static Setter
The Singleton Design Pattern
Steps
Link Substitution
Parameterize Constructor
Steps
Parameterize Method
Steps
Primitivize Parameter
Steps
Pull Up Feature
Push Down Dependency
Replace Function with Function Pointer
Steps
Replace Global Reference with Getter
Subclass and Override Method
Supersede Instance Variable
Steps
Template Redefinition
Steps
Text Redefinition
Steps
Appendix. Refactoring
Extract Method
Glossary
Table of Contents Robert C. Martin Series...........................................................................................................................1 Foreword..................................................................................................................................................1 Preface......................................................................................................................................................1 .................................................................................................................................................................2 Acknowledgments.............................................................................................................................3 Introduction..............................................................................................................................................5 How to Use This Book......................................................................................................................5 Part I: The Mechanics of Change......................................................................................................................6 Chapter 1. Changing Software.................................................................................................................6 Four Reasons to Change Software....................................................................................................8 .................................................................................................................................................................8 Improving Design..............................................................................................................................9 .................................................................................................................................................................9 Risky Change....................................................................................................................................9 Chapter 2. Working with Feedback.......................................................................................................10 Software Vise.........................................................................................................................................10 What Is Unit Testing?......................................................................................................................11 Test Harnesses.......................................................................................................................................12 ...............................................................................................................................................................12...............................................................................................................................................................13...............................................................................................................................................................13 Higher-Level Testing......................................................................................................................15 Test Coverings.................................................................................................................................16 ...............................................................................................................................................................19 The Legacy Code Dilemma...................................................................................................................19 Figure 2.2. Invoice update classes with dependencies broken........................................................21 ...............................................................................................................................................................21 The Legacy Code Change Algorithm..............................................................................................22 Chapter 3. Sensing and Separation........................................................................................................23 Faking Collaborators.......................................................................................................................24 Fake Objects Support Real Tests...........................................................................................................27 The Two Sides of a Fake Object.....................................................................................................27 Chapter 4. The Seam Model..................................................................................................................27 A Huge Sheet of Text......................................................................................................................28 Seams...............................................................................................................................................30 Seam.......................................................................................................................................................31 Seam Types.....................................................................................................................................34 Seam.......................................................................................................................................................35 Enabling Point........................................................................................................................................35 Link Seams......................................................................................................................................35 ...............................................................................................................................................................37 Usage Tip...............................................................................................................................................37 Object Seams...................................................................................................................................41 Chapter 5. Tools.......................................................................................................................................1 Automated Refactoring Tools...........................................................................................................1 .................................................................................................................................................................2 Tests and Automated Refactoring............................................................................................................3 Mock Objects....................................................................................................................................6 Unit-Testing Harnesses...................................................................................................................10 General Test Harnesses...................................................................................................................12 Part II: Changing Software..............................................................................................................................14 Chapter 6. I Don't Have Much Time and I Have to Change It..............................................................17 It Happens Someplace Every Day.........................................................................................................17 Sprout Method.................................................................................................................................18 Sprout Class.....................................................................................................................................18 Wrap Method...................................................................................................................................19 Wrap Class......................................................................................................................................23 The Decorator Pattern............................................................................................................................23 Summary.........................................................................................................................................24 Chapter 7. It Takes Forever to Make a Change.....................................................................................24 i
Table of Contents Part II: Changing Software Understanding.................................................................................................................................24 Lag Time.........................................................................................................................................25 Breaking Dependencies...................................................................................................................26 The Dependency Inversion Principle.....................................................................................................26 Figure 7.5. Package structure..........................................................................................................30 ...............................................................................................................................................................31 Summary.........................................................................................................................................37 Chapter 8. How Do I Add a Feature?....................................................................................................37 Test-Driven Development (TDD)...................................................................................................38 ...............................................................................................................................................................39 Remove Duplication........................................................................................................................39 TDD and Legacy Code..........................................................................................................................40 Programming by Difference............................................................................................................41 ...............................................................................................................................................................42 The Liskov Substitution Principle.........................................................................................................44 Figure 8.7. Normalized hierarchy....................................................................................................45 Summary.........................................................................................................................................45 Chapter 9. I Can't Get This Class into a Test Harness...........................................................................46 The Case of the Irritating Parameter...............................................................................................49 ...............................................................................................................................................................51 Figure 9.1. RGHConnection............................................................................................................53 Test Code vs. Production Code..............................................................................................................57 Pass Null................................................................................................................................................60 Null Object Pattern................................................................................................................................62 The Case of the Hidden Dependency..............................................................................................65 The Case of the Construction Blob.................................................................................................65 The Case of the Irritating Global Dependency................................................................................67 ...............................................................................................................................................................68 The Case of the Horrible Include Dependencies.............................................................................68 The Case of the Onion Parameter....................................................................................................71 The Case of the Aliased Parameter.................................................................................................73 Chapter 10. I Can't Run This Method in a Test Harness.......................................................................75 The Case of the Hidden Method......................................................................................................76 ...............................................................................................................................................................77 Subverting Access Protection................................................................................................................77 The Case of the "Helpful" Language Feature.................................................................................78 The Case of the Undetectable Side Effect.......................................................................................78 Command/Query Separation..................................................................................................................80 Figure 10.1. AccountDetailFrame...................................................................................................82 Chapter 11. I Need to Make a Change. What Methods Should I Test?.................................................85 Reasoning About Effects.................................................................................................................86 IDE Support for Effect Analysis............................................................................................................87 ...............................................................................................................................................................87 Figure 11.1. declarations impacts geTDeclarationCount................................................................88 ...............................................................................................................................................................88 Reasoning Forward..........................................................................................................................90 ...............................................................................................................................................................90 Figure 11.9. Effects through the Element class...............................................................................91 Effect Propagation...........................................................................................................................93 ...............................................................................................................................................................93...............................................................................................................................................................94 Tools for Effect Reasoning..............................................................................................................94 ...............................................................................................................................................................97 Learning from Effect Analysis........................................................................................................97 Simplifying Effect Sketches..........................................................................................................100 Effects and Encapsulation....................................................................................................................101 Chapter 12. I Need to Make Many Changes in One Area. Do I Have to Break Dependencies for All the Classes Involved?....................................................................................................................101 .............................................................................................................................................................102 Interception Points.........................................................................................................................103 .............................................................................................................................................................103 Higher-Level Interception Points..................................................................................................105 Pinch Point...........................................................................................................................................106 Judging Design with Pinch Points.................................................................................................106 Using Effect Sketches to Find Hidden Classes....................................................................................106 ii
Table of Contents Part II: Changing Software Pinch Point Traps..........................................................................................................................107 Chapter 13. I Need to Make a Change, but I Don't Know What Tests to Write..................................109 Characterization Tests...................................................................................................................110 .............................................................................................................................................................110 The Method Use Rule..........................................................................................................................111 Characterizing Classes..................................................................................................................111 When You Find Bugs..........................................................................................................................112 Targeted Testing............................................................................................................................112 .............................................................................................................................................................112.............................................................................................................................................................113.............................................................................................................................................................113 Refactoring Tool Quirks......................................................................................................................116 A Heuristic for Writing Characterization Tests............................................................................119 Chapter 14. Dependencies on Libraries Are Killing Me.....................................................................120 .............................................................................................................................................................121.............................................................................................................................................................122.............................................................................................................................................................123 Chapter 15. My Application Is All API Calls......................................................................................123 Figure 15.1. A better mailing list server........................................................................................124 Chapter 16. I Don't Understand the Code Well Enough to Change It.................................................127 Notes/Sketching.............................................................................................................................129 Listing Markup..............................................................................................................................130 Scratch Refactoring.......................................................................................................................130 Delete Unused Code......................................................................................................................131 Chapter 17. My Application Has No Structure....................................................................................132 Telling the Story of the System.....................................................................................................133 Naked CRC....................................................................................................................................134 Conversation Scrutiny...................................................................................................................137 Chapter 18. My Test Code Is in the Way.............................................................................................139 Class Naming Conventions...........................................................................................................142 Test Location.................................................................................................................................144 Chapter 19. My Project Is Not Object Oriented. How Do I Make Safe Changes?..............................145 An Easy Case.................................................................................................................................145 A Hard Case..................................................................................................................................147 Adding New Behavior...................................................................................................................147 Taking Advantage of Object Orientation......................................................................................148 It's All Object Oriented.................................................................................................................148 Chapter 20. This Class Is Too Big and I Don't Want It to Get Any Bigger........................................148 Single-Responsibility Principle (SRP).................................................................................................149 Figure 20.1. Rule parser................................................................................................................150 Seeing Responsibilities..................................................................................................................151 Heuristic #1: Group Methods...............................................................................................................152 Heuristic #2: Look at Hidden Methods................................................................................................156 Figure 20.3. RuleParser and TermTokenizer................................................................................156 Heuristic #3: Look for Decisions That Can Change............................................................................158 Heuristic #4: Look for Internal Relationships.....................................................................................158 Figure 20.4. Variables in the Reservation class............................................................................159 .............................................................................................................................................................159 Figure 20.6. Feature sketch for Reservation..................................................................................160 Heuristic #5: Look for the Primary Responsibility..............................................................................160 Figure 20.11. The ScheduledJob class..........................................................................................162 Interface Segregation Principle (ISP)..................................................................................................162 Figure 20.14. Segregating the interface of ScheduledJob.............................................................164 Heuristic #6: When All Else Fails, Do Some Scratch Refactoring......................................................165 Heuristic #7: Focus on the Current Work............................................................................................166 Other Techniques..........................................................................................................................167 Moving Forward............................................................................................................................168 After Extract Class........................................................................................................................170 Chapter 21. I'm Changing the Same Code All Over the Place............................................................176 iii
Table of Contents Part II: Changing Software Figure 21.1. AddEmployeeCmd and LoginCommand..................................................................178 First Steps......................................................................................................................................178 Deciding Where to Start.......................................................................................................................179 Figure 21.2. Command hierarchy..................................................................................................182 .............................................................................................................................................................183 Figure 21.3. Pulling up writeField.................................................................................................184 Abbreviations.......................................................................................................................................185 Open/Closed Principle.........................................................................................................................190 Chapter 22. I Need to Change a Monster Method and I Can't Write Tests for It................................193 Varieties of Monsters....................................................................................................................193 Tackling Monsters with Automated Refactoring Support............................................................194 .............................................................................................................................................................194 Figure 22.4. Logic class extracted from CommoditySelectionPanel............................................195 The Manual Refactoring Challenge..............................................................................................198 Strategy..........................................................................................................................................200 Chapter 23. How Do I Know That I'm Not Breaking Anything?............................................................1 Hyperaware Editing...........................................................................................................................1 .................................................................................................................................................................1 Single-Goal Editing...........................................................................................................................1 Preserve Signatures...........................................................................................................................2 Lean on the Compiler........................................................................................................................3 Chapter 24. We Feel Overwhelmed. It Isn't Going to Get Any Better....................................................4 ...............................................................................................................................................................17...............................................................................................................................................................17...............................................................................................................................................................18...............................................................................................................................................................19...............................................................................................................................................................19...............................................................................................................................................................21 ...............................................................................................................................................................26...............................................................................................................................................................26 ...............................................................................................................................................................32...............................................................................................................................................................34 ...............................................................................................................................................................22...............................................................................................................................................................23 Part III: Dependency-Breaking Techniques....................................................................................................4 Chapter 25. Dependency-Breaking Techniques......................................................................................4 .................................................................................................................................................................5 Adapt Parameter................................................................................................................................9 .................................................................................................................................................................9...............................................................................................................................................................10...............................................................................................................................................................12...............................................................................................................................................................12 Steps................................................................................................................................................13 Break Out Method Object...............................................................................................................13 ...............................................................................................................................................................14 Steps................................................................................................................................................15 Definition Completion.....................................................................................................................16 Encapsulate Global References.......................................................................................................16 Steps................................................................................................................................................21 Expose Static Method......................................................................................................................22 Steps................................................................................................................................................24 Extract and Override Call................................................................................................................25 Extract and Override Factory Method.............................................................................................25 Steps................................................................................................................................................28 Extract and Override Getter............................................................................................................30 Steps................................................................................................................................................34 Extract Implementer........................................................................................................................34 ...............................................................................................................................................................36 Steps................................................................................................................................................38 Extract Interface..............................................................................................................................38 Interface Naming...................................................................................................................................40 ...............................................................................................................................................................42 Steps................................................................................................................................................42 Extract Interface and Non-Virtual Functions.........................................................................................43 Introduce Instance Delegator...........................................................................................................46 Introduce Static Setter.....................................................................................................................46 The Singleton Design Pattern................................................................................................................47 ...............................................................................................................................................................47 Steps................................................................................................................................................48 Link Substitution.............................................................................................................................48 Parameterize Constructor................................................................................................................50 ...............................................................................................................................................................50 Steps................................................................................................................................................50 Parameterize Method.......................................................................................................................53 ...............................................................................................................................................................54 Steps................................................................................................................................................56 iv
Table of Contents Part III: Dependency-Breaking Techniques Primitivize Parameter......................................................................................................................58 ...............................................................................................................................................................59 Steps................................................................................................................................................59 Pull Up Feature................................................................................................................................61 ...............................................................................................................................................................64 Push Down Dependency.................................................................................................................66 Replace Function with Function Pointer.........................................................................................66 ...............................................................................................................................................................67 Steps................................................................................................................................................68 Replace Global Reference with Getter............................................................................................69 Subclass and Override Method........................................................................................................70 Supersede Instance Variable...........................................................................................................70 ...............................................................................................................................................................71 Steps................................................................................................................................................71 Template Redefinition.....................................................................................................................71 ...............................................................................................................................................................72 Steps................................................................................................................................................74 Text Redefinition.............................................................................................................................75 .............................................................................................................................................................title Steps..............................................................................................................................................title Appendix. Refactoring.........................................................................................................................title Extract Method..............................................................................................................................title .............................................................................................................................................................title.............................................................................................................................................................title Glossary...............................................................................................................................................title v
vi
分享到:
收藏