You are previewing Refactoring Workbook.
O'Reilly logo
Refactoring Workbook

Book Description

As a programmer, you need to be able to recognize and improve problematic code, so the program remains in a working state throughout the software lifecycle. Refactoring-the art of improving the design of existing code safely-provides an efficient, reliable system for bringing order to the chaos, and one that keeps the surprises to a minimum! Refactoring can be difficult to learn, but Refactoring Workbook, by consultant William C. Wake, presents the material in a easy-learning format that makes learning enjoyable and effective.

For many, the obstacle to learning refactoring is in identifying the "smells"—the potential problem areas-found in code. Instead of having you read about the smells, Refactoring Workbook makes sure you understand them. You'll solve a carefully assembled series of problems, and you'll find yourself learning at a deeper level and arriving at a few insights of your own. Wake uses the workbook method—a learning-focused approach that forces you to apply the techniques presented in the book-in the rest of the book. This approach helps you learn and apply the most important refactoring techniques to your code and, as a side benefit, helps you to think more about creating great code even when you're not refactoring.

Refactoring Workbook provides user-friendly references such as:

  • A handy, quick-reference "smell finder"

  • A standard format for describing smells

  • Appendices showing key refactorings

  • A listing of Java™ tools that support refactoring

This book is intended for programmers with a knowledge of Java, though a C# or C++ programmer with a basic understanding of Java would also be able to follow and learn from the examples. It can be used as a companion to Martin Fowler's Refactoring (also from Addison-Wesley Professional), which provides step-by-step instructions for many refactorings.

Table of Contents

  1. Copyright
    1. Dedication
  2. Preface
    1. What Is Refactoring?
    2. What Are the Goals of This Book?
    3. Who Is This Book For?
    4. What Background Do You Need?
    5. How to Use This Book
    6. Can I Contact the Author?
    7. Acknowledgments
  3. 1. Roadmap
    1. Overview
    2. Section 1: Smells within Classes
    3. Section 2: Smells between Classes
    4. Section 3: Programs to Refactor
    5. A Word on the Challenges
  4. 1. Smells Within Classes
    1. 2. The Refactoring Cycle
      1. What Is Refactoring?
      2. Smells Are Problems
      3. The Refactoring Cycle
      4. When Are We Done?
      5. Inside a Refactoring
        1. Step 1: Create getter and setter methods.
        2. Step 2: Find all clients; replace references with calls. Do this one at a time.
        3. Step 3: Compile and test.
        4. Step 2: Replace the other reference.
        5. Step 3: Compile and test.
        6. Step 4: Once all clients are changed, make the field private.
        7. Step 5: Compile and test one last time.
      6. Challenges
      7. Conclusion
    2. 3. Measured Smells
      1. Smells Covered
      2. Comments
        1. Symptoms
        2. Causes
        3. What to Do
        4. Payoff
        5. Contraindications
      3. Long Method
        1. Symptoms
        2. Causes
        3. What to Do
        4. Payoff
        5. Discussion
        6. Contraindications
      4. Large Class
        1. Symptoms
        2. Causes
        3. What to Do
        4. Payoff
      5. Long Parameter List
        1. Symptoms
        2. Causes
        3. What to Do
        4. Payoff
        5. Contraindications
        6. Notes
      6. More Challenges
      7. Conclusion
    3. 1. Smells and Refactorings
    4. 4. Names
      1. Smells Covered
      2. Type Embedded in Name (Including Hungarian)
        1. Symptoms
        2. How It Got This Way
        3. What to Do
        4. Payoff
        5. Contraindications
      3. Uncommunicative Name
        1. Symptoms
        2. Causes
        3. What to Do
        4. Payoff
        5. Contraindications
      4. Inconsistent Names
        1. Symptoms
        2. Causes
        3. What to Do
        4. Payoff
        5. Notes
    5. 5. Unnecessary Complexity
      1. Smells Covered
      2. Dead Code
        1. Symptoms
        2. Causes
        3. What to Do
        4. Payoff
        5. Contraindications
        6. Notes
      3. Speculative Generality
        1. Symptoms
        2. Causes
        3. What to Do
        4. Payoff
        5. Contraindications
    6. 2. Inverses
    7. 6. Duplication
      1. Smells Covered
      2. Magic Number
        1. Symptoms
        2. Causes
        3. What to Do
        4. Payoff
        5. Contraindications
      3. Duplicated Code
        1. Symptoms
        2. Causes
        3. What to Do
        4. Payoff
        5. Contraindications
      4. Alternative Classes with Different Interfaces
        1. Symptoms
        2. Causes
        3. What to Do
        4. Payoff
        5. Contraindications
      5. Challenges
    8. 7. Conditional Logic
      1. Smells Covered
      2. Null Check
        1. Symptoms
        2. Causes
        3. What to Do
        4. Payoff
        5. Contraindications
      3. Complicated Boolean Expression
        1. Symptoms
        2. Causes
        3. What to Do
        4. Payoff
        5. Contraindications
      4. Special Case
        1. Symptoms
        2. Causes
        3. What to Do
        4. Payoff
        5. Contraindications
      5. Simulated Inheritance (Switch Statement)
        1. Symptoms
        2. Causes
        3. What to Do
        4. Payoff
        5. Contraindications
    9. 3. Design Patterns
  5. 2. Smells Between Classes
    1. 8. Data
      1. Smells Covered
      2. Primitive Obsession
        1. Symptoms
        2. Causes
        3. What to Do
        4. Payoff
        5. Contraindications
        6. Notes
      3. Data Class
        1. Symptoms
        2. Causes
        3. What to Do
        4. Payoff
        5. Contraindications
      4. Data Clump
        1. Symptoms
        2. Causes
        3. What to Do
        4. Payoff
        5. Contraindications
      5. Temporary Field
        1. Symptoms
        2. Causes
        3. What to Do
        4. Payoff
        5. Contraindications
    2. 9. Inheritance
      1. Smells Covered
      2. Refused Bequest
        1. Symptoms
        2. Causes
        3. What to Do
        4. Payoff
        5. Contraindications
        6. Notes
      3. Inappropriate Intimacy (Subclass Form)
        1. Symptoms
        2. Causes
        3. What to Do
        4. Payoff
        5. Contraindications
      4. Lazy Class
        1. Symptoms
        2. Causes
        3. What to Do
        4. Payoff
        5. Contraindications
    3. 10. Responsibility
      1. Smells Covered
      2. Feature Envy
        1. Symptoms
        2. Causes
        3. What to Do
        4. Payoff
        5. Contraindications
        6. Notes
      3. Inappropriate Intimacy (General Form)
        1. Symptoms
        2. Causes
        3. What to Do
        4. Payoff
        5. Contraindications
      4. Message Chains
        1. Symptoms
        2. Causes
        3. What to Do
        4. Payoff
        5. Contraindications
      5. Middle Man
        1. Symptoms
        2. Causes
        3. What to Do
        4. Payoff
        5. Contraindications
      6. Challenges
    4. 11. Accommodating Change
      1. Smells Covered
      2. Divergent Change
        1. Symptoms
        2. Causes
        3. What to Do
        4. Payoff
        5. Contraindications
      3. Shotgun Surgery
        1. Symptoms
        2. Causes
        3. What to Do
        4. Payoff
        5. Contraindications
      4. Parallel Inheritance Hierarchies
        1. Symptoms
        2. Causes
        3. What to Do
        4. Payoff
        5. Contraindications
      5. Combinatorial Explosion
        1. Symptoms
        2. Causes
        3. What to Do
        4. Payoff
        5. Contraindications
    5. 12. Library Classes
      1. Smells Covered
      2. Incomplete Library Class
        1. Symptoms
        2. Causes
        3. What to Do
        4. Payoff
        5. Contraindications
      3. Challenges
    6. 4. Gen-A-Refactoring
  6. 3. Programs to Refactor
    1. 13. A Database Example
      1. Course.java (Online at www.xp123.com/rwb)
      2. Offering.java
      3. Schedule.java
      4. Report.java
      5. TestSchedule.java
      6. TestReport.java
    2. 14. A Simple Game
      1. Development Episodes
    3. 15. Catalog
      1. Introduction
      2. Path 1: Catalog.itemsMatching(query)
      3. Path 2: Query.matchesIn(catalog)
      4. Path 3: Process(catalog.data, query.data)
      5. Conclusion
    4. 16. Planning Game Simulator
      1. Part 1: Original Code
        1. Code (Online at www.xp123.com/rwb)Table.java
        2. Background.java
        3. Card.java
        4. Challenges
      2. Part 2: Redistributing Features
        1. Five Whys
      3. Removing Duplication, Selection Troubles, and a Few Burrs
      4. Part 3: Pushing the Code Further
    5. 17. Where to Go from Here
      1. Books
      2. Admonitions
        1. Build Refactoring into Your Practice
        2. Build Testing into Your Practice
        3. Get Help from Others
      3. Exercises to Try
        1. Smell Scavenger Hunt/Smell of the Week
        2. Re-Refactor
        3. Just Refactor
        4. Inhale/Exhale
        5. Defactoring/Malfactoring
        6. Refactoring Kata
      4. Web Sites
  7. 4. Appendixes
    1. A. Answers to Selected Questions
      1. Chapter 2. The Refactoring Cycle
      2. Chapter 3. Measured Smells
      3. Interlude 1. Smells and Refactorings
      4. Chapter 4. Names
      5. Chapter 5. Unnecessary Complexity
      6. Chapter 6. Duplication
      7. Chapter 7. Conditional Logic
      8. Interlude 3. Design Patterns
      9. Chapter 8. Data
      10. Chapter 9. Inheritance
      11. Chapter 10. Responsibility
      12. Chapter 11. Accommodating Change
      13. Chapter 12. Library Classes
      14. Interlude 4. Gen-A-Refactoring
      15. Chapter 13. A Database Example
      16. Chapter 14. A Simple Game
      17. Chapter 15. Catalog
      18. Chapter 16. Planning Game
    2. B. Java Refactoring Tools
    3. C. Inverses for Refactorings
    4. D. Key Refactorings
      1. Change Bidirectional Association to Unidirectional
      2. Duplicate Observed Data
      3. Extract Class
      4. Extract Method
      5. Move Field
      6. Move Method
      7. Replace Type Code with State/Strategy
  8. Bibliography