O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Refactoring: Improving the Design of Existing Code, Second Edition

Book Description

Fully Revised and Updated–Includes New Refactorings and Code Examples

 

“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.”
—M. Fowler (1999)

 

For more than twenty years, experienced programmers worldwide have relied on Martin Fowler's Refactoring to improve the design of existing code and to enhance software maintainability, as well as to make existing code easier to understand.

 

This eagerly awaited new edition has been fully updated to reflect crucial changes in the programming landscape. Refactoring, Second Edition, features an updated catalog of refactorings and includes JavaScript code examples, as well as new functional examples that demonstrate refactoring without classes.

 

Like the original, this edition explains what refactoring is; why you should refactor; how to recognize code that needs refactoring; and how to actually do it successfully, no matter what language you use.

  • Understand the process and general principles of refactoring
  • Quickly apply useful refactorings to make a program easier to comprehend and change
  • Recognize “bad smells” in code that signal opportunities to refactor
  • Explore the refactorings, each with explanations, motivation, mechanics, and simple examples
  • Build solid tests for your refactorings
  • Recognize tradeoffs and obstacles to refactoring

Includes free access to the canonical web edition, with even more refactoring resources. (See inside the book for details about how to access the web edition.)

Table of Contents

  1. Cover Page
  2. Title Page
  3. Contents at a Glance
  4. Contents
  5. Foreword to the First Edition
  6. Preface
    1. What Is Refactoring?
    2. What’s in This Book?
    3. Who Should Read This Book?
    4. Building on a Foundation Laid by Others
    5. Acknowledgments
  7. Chapter 1: Refactoring: A First Example
    1. The Starting Point
    2. Comments on the Starting Program
    3. The First Step in Refactoring
    4. Decomposing the statement Function
    5. Status: Lots of Nested Functions
    6. Splitting the Phases of Calculation and Formatting
    7. Status: Separated into Two Files (and Phases)
    8. Reorganizing the Calculations by Type
    9. Status: Creating the Data with the Polymorphic Calculator
    10. Final Thoughts
  8. Chapter 2: Principles in Refactoring
    1. Defining Refactoring
    2. The Two Hats
    3. Why Should We Refactor?
    4. When Should We Refactor?
    5. Problems with Refactoring
    6. Refactoring, Architecture, and Yagni
    7. Refactoring and the Wider Software Development Process
    8. Refactoring and Performance
    9. Where Did Refactoring Come From?
    10. Automated Refactorings
    11. Going Further
  9. Chapter 3: Bad Smells in Code
    1. Mysterious Name
    2. Duplicated Code
    3. Long Function
    4. Long Parameter List
    5. Global Data
    6. Mutable Data
    7. Divergent Change
    8. Shotgun Surgery
    9. Feature Envy
    10. Data Clumps
    11. Primitive Obsession
    12. Repeated Switches
    13. Loops
    14. Lazy Element
    15. Speculative Generality
    16. Temporary Field
    17. Message Chains
    18. Middle Man
    19. Insider Trading
    20. Large Class
    21. Alternative Classes with Different Interfaces
    22. Data Class
    23. Refused Bequest
    24. Comments
  10. Chapter 4: Building Tests
    1. The Value of Self-Testing Code
    2. Sample Code to Test
    3. A First Test
    4. Add Another Test
    5. Modifying the Fixture
    6. Probing the Boundaries
    7. Much More Than This
  11. Chapter 5: Introducing the Catalog
    1. Format of the Refactorings
    2. The Choice of Refactorings
  12. Chapter 6: A First Set of Refactorings
    1. Extract Function
    2. Inline Function
    3. Extract Variable
    4. Inline Variable
    5. Change Function Declaration
    6. Encapsulate Variable
    7. Rename Variable
    8. Introduce Parameter Object
    9. Combine Functions into Class
    10. Combine Functions into Transform
    11. Split Phase
  13. Chapter 7: Encapsulation
    1. Encapsulate Record
    2. Encapsulate Collection
    3. Replace Primitive with Object
    4. Replace Temp with Query
    5. Extract Class
    6. Inline Class
    7. Hide Delegate
    8. Remove Middle Man
    9. Substitute Algorithm
  14. Chapter 8: Moving Features
    1. Move Function
    2. Move Field
    3. Move Statements into Function
    4. Move Statements to Callers
    5. Replace Inline Code with Function Call
    6. Slide Statements
    7. Split Loop
    8. Replace Loop with Pipeline
    9. Remove Dead Code
  15. Chapter 9: Organizing Data
    1. Split Variable
    2. Rename Field
    3. Replace Derived Variable with Query
    4. Change Reference to Value
    5. Change Value to Reference
  16. Chapter 10: Simplifying Conditional Logic
    1. Decompose Conditional
    2. Consolidate Conditional Expression
    3. Replace Nested Conditional with Guard Clauses
    4. Replace Conditional with Polymorphism
    5. Introduce Special Case
    6. Introduce Assertion
  17. Chapter 11: Refactoring APIs
    1. Separate Query from Modifier
    2. Parameterize Function
    3. Remove Flag Argument
    4. Preserve Whole Object
    5. Replace Parameter with Query
    6. Replace Query with Parameter
    7. Remove Setting Method
    8. Replace Constructor with Factory Function
    9. Replace Function with Command
    10. Replace Command with Function
  18. Chapter 12: Dealing with Inheritance
    1. Pull Up Method
    2. Pull Up Field
    3. Pull Up Constructor Body
    4. Push Down Method
    5. Push Down Field
    6. Replace Type Code with Subclasses
    7. Remove Subclass
    8. Extract Superclass
    9. Collapse Hierarchy
    10. Replace Subclass with Delegate
    11. Replace Superclass with Delegate
  19. Bibliography