#atom

Common strategies for improving code structure without changing behavior

Core Idea: Refactoring patterns are systematic approaches to restructuring code that improve design, readability, and maintainability while preserving existing functionality.

Key Elements

Common Refactoring Patterns

  1. Extract Method/Function

    • Moves code fragment into a separate named method
    • Improves readability and reduces duplication
    • Creates potential for reuse
  2. Rename Variable/Method/Class

    • Changes identifier to better reflect purpose
    • Improves code readability and self-documentation
    • Crucial for maintaining conceptual integrity
  3. Move Method/Function

    • Relocates method to a more appropriate class or module
    • Improves cohesion and reduces coupling
    • Helps maintain single responsibility principle
  4. Replace Conditional with Polymorphism

    • Substitutes conditional logic with class hierarchy
    • Makes code more extensible
    • Allows for cleaner type-specific behavior
  5. Inline Method/Temp Variable

    • Removes unnecessary abstraction or intermediate variable
    • Simplifies code when abstraction provides no value
    • Often used when unwinding inappropriate abstractions
  6. Introduce Parameter Object

    • Groups related parameters into a single object
    • Simplifies method signatures
    • Creates natural home for behavior related to those parameters

When to Refactor

Connections

References

  1. Fowler, Martin. "Refactoring: Improving the Design of Existing Code"
  2. Feathers, Michael. "Working Effectively with Legacy Code"

#programming #refactoring #software_design #clean_code


Connections:


Sources: