logo资料库

Working Effectively with Legacy Code.pdf

第1页 / 共458页
第2页 / 共458页
第3页 / 共458页
第4页 / 共458页
第5页 / 共458页
第6页 / 共458页
第7页 / 共458页
第8页 / 共458页
资料共458页,剩余部分请下载后查看
Contents
Foreword
Preface
Introduction
PART I: The Mechanics of Change
Chapter 1: Changing Software
Four Reasons to Change Software
Risky Change
Chapter 2: Working with Feedback
What Is Unit Testing?
Higher-Level Testing
Test Coverings
The Legacy Code Change Algorithm
Chapter 3: Sensing and Separation
Faking Collaborators
Chapter 4: The Seam Model
A Huge Sheet of Text
Seams
Seam Types
Chapter 5: Tools
Automated Refactoring Tools
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
Sprout Method
Sprout Class
Wrap Method
Wrap Class
Summary
Chapter 7: It Takes Forever to Make a Change
Understanding
Lag Time
Breaking Dependencies
Summary
Chapter 8: How Do I Add a Feature?
Test-Driven Development (TDD)
Programming by Difference
Summary
Chapter 9: I Can’t Get This Class into a Test Harness
The Case of the Irritating Parameter
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
The Case of the “Helpful” Language Feature
The Case of the Undetectable Side Effect
Chapter 11: I Need to Make a Change. What Methods Should I Test?
Reasoning About Effects
Reasoning Forward
Effect Propagation
Tools for Effect Reasoning
Learning from Effect Analysis
Simplifying Effect Sketches
Chapter 12: I Need to Make Many Changes in One Area
Interception Points
Judging Design with Pinch Points
Pinch Point Traps
Chapter 13: I Need to Make a Change, but I Don’t Know What Tests to Write
Characterization Tests
Characterizing Classes
Targeted Testing
A Heuristic for Writing Characterization Tests
Chapter 14: Dependencies on Libraries Are Killing Me
Chapter 15: My Application Is All API Calls
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
Seeing Responsibilities
Other Techniques
Moving Forward
After Extract Class
Chapter 21: I’m Changing the Same Code All Over the Place
First Steps
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
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
Break Out Method Object
Definition Completion
Encapsulate Global References
Expose Static Method
Extract and Override Call
Extract and Override Factory Method
Extract and Override Getter
Extract Implementer
Extract Interface
Introduce Instance Delegator
Introduce Static Setter
Link Substitution
Parameterize Constructor
Parameterize Method
Primitivize Parameter
Pull Up Feature
Push Down Dependency
Replace Function with Function Pointer
Replace Global Reference with Getter
Subclass and Override Method
Supersede Instance Variable
Template Redefinition
Text Redefinition
Appendix: Refactoring
Extract Method
Glossary
Index
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
R
S
T
U
V
W
X
ptg9926858From the Library of Brian Watterson
Working Effectively with Legacy Code
Robert C. Martin Series This series is directed at software developers, team-leaders, business analysts, and managers who want to increase their skills and proficiency to the level of a Master Craftsman. The series contains books that guide software professionals in the principles, patterns, and practices of programming, software project management, requirements gathering, design, analysis, testing, and others. ptg9926858From the Library of Brian Watterson
Working Effectively with Legacy Code Michael C. Feathers Prentice Hall Professional Technical Reference Upper Saddle River, NJ 07458 www,phptr.com ptg9926858From the Library of Brian Watterson
The authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibil- ity for errors or omissions. No liability is assumed for incidental or consequen- tial damages in connection with or arising out of the use of the information or programs contained herein. Publisher: John Wait Editor in Chief: Don O’Hagan Acquisitions Editor: Paul Petralia Editorial Assistant: Michelle Vincenti Marketing Manager: Chris Guzikowski Publicist: Kerry Guiliano Cover Designer: Sandra Schroeder Managing Editor: Gina Kanouse Senior Project Editor: Lori Lyons Copy Editor: Krista Hansing Indexer: Lisa Stumpf Compositor: Karen Kennedy Proofreader: Debbie Williams Manufacturing Buyer: Dan Uhrig Prentice Hall offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding in- terests. For more information, please contact: U. S. Corporate and Government Sales 1-800-382-3419 corpsales@pearsontechgroup.com For sales outside the U. S., please contact: International Sales 1-317-428-3341 international@pearsontechgroup.com Visit us on the web: www.phptr.com Library of Congress Cataloging-in-Publication Data: 2004108115 Copyright © 2005 Pearson Education, Inc. Publishing as Prentice Hall PTR All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, write to: Pearson Education, Inc. Rights and Contracts Department One Lake Street Upper Saddle River, NJ 07458 Other product or company names mentioned herein are the trademarks or registered trademarks of their respective owners. ISBN 0-13-117705-2 Text printed in the United States on recycled paper at Phoenix Book Tech. First printing, September 2004 ptg9926858From the Library of Brian Watterson
For Ann, Deborah, and Ryan, the bright centers of my life. — Michael ptg9926858From the Library of Brian Watterson
This page intentionally left blank ptg9926858From the Library of Brian Watterson
CONTENTS vii Contents Foreword by Robert C. Martin. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi PART I: The Mechanics of Change. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Chapter 1: Changing Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Four Reasons to Change Software . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Risky Change . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Chapter 2: Working with Feedback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 What Is Unit Testing? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Higher-Level Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Test Coverings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 The Legacy Code Change Algorithm . . . . . . . . . . . . . . . . . . . . . . . 18 Chapter 3: Sensing and Separation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Faking Collaborators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Chapter 4: The Seam Model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 A Huge Sheet of Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Seams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Seam Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Chapter 5: Tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Automated Refactoring Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Mock Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Unit-Testing Harnesses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 General Test Harnesses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 ptg9926858From the Library of Brian Watterson
分享到:
收藏