Skip to content

Latest commit

 

History

History
79 lines (61 loc) · 3.42 KB

File metadata and controls

79 lines (61 loc) · 3.42 KB

Refactoring: Improving the Design of Existing Code by Martin Fowler

Introduction

Refactoring by Martin Fowler is a foundational book that introduces the concept of refactoring, a disciplined technique for improving existing code without changing its external behavior. It provides a catalog of common refactorings and explains when and why to apply them.


Part 1: Understanding Refactoring

1. What Is Refactoring?

  • Refactoring: The process of improving code structure without altering functionality.
  • Key benefits:
    • Improves readability and maintainability.
    • Reduces technical debt.
    • Simplifies debugging and feature additions.

2. When to Refactor

  • Rule of Three: If you duplicate code a third time, refactor.
  • Before adding new functionality.
  • During code reviews when encountering unclear or messy code.
  • After identifying performance bottlenecks (but only if necessary).

3. The Refactoring Process

  • Steps:

    1. Ensure the code is covered by tests.
    2. Make small, incremental changes.
    3. Run tests frequently to confirm behavior remains unchanged.
  • Bad smells (indicators that refactoring is needed):

    • Duplicated code
    • Long methods
    • Large classes
    • Too many parameters
    • Inconsistent naming

Part 2: The Refactoring Catalog

4. Composing Methods

  • Extract Method: Move a block of code into its own method to improve readability.
  • Inline Method: Remove unnecessary methods that only call other methods.
  • Replace Temp with Query: Use a method instead of a temporary variable for better clarity.

5. Moving Features Between Objects

  • Move Method: Shift a method to the class where it fits best.
  • Extract Class: Split a large class into multiple smaller, focused classes.
  • Introduce Parameter Object: Replace long parameter lists with an object.

6. Simplifying Conditional Expressions

  • Decompose Conditional: Break down complex conditionals into separate methods.
  • Replace Nested Conditionals with Guard Clauses: Handle special cases first to improve readability.
  • Replace Type Code with Polymorphism: Use class hierarchies instead of case statements.

7. Refactoring Classes

  • Extract Superclass: Create a parent class when multiple classes share similar behavior.
  • Extract Interface: Define an interface for shared behavior instead of relying on implementation details.
  • Replace Inheritance with Delegation: Use composition instead of deep inheritance trees.

Part 3: Refactoring in Practice

8. Refactoring and Testing

  • Automated tests are essential to ensure refactoring doesn’t introduce bugs.
  • Use unit tests and regression tests before and after refactoring.
  • Test-driven development (TDD) encourages continuous refactoring.

9. Performance Considerations

  • Don’t optimize prematurely—focus on clarity first.
  • Only refactor for performance after measuring bottlenecks.

10. When Not to Refactor

  • When code is already stable and rarely changes.
  • If deadlines are extremely tight (though this leads to technical debt).
  • When rewriting is a better option than incremental refactoring.

Conclusion

Refactoring teaches developers how to systematically improve code quality while keeping it functional. By recognizing bad code smells and applying proven refactoring techniques, developers can make their code easier to read, modify, and extend over time.