Key Refactoring Techniques

Key Refactoring Techniques

Here are fundamental refactoring techniques that address common design smells.

Composing Methods

  • Extract Method: Turns a cohesive sub-block of code from a long method into a cleanly named independent method.
  • Inline Method: Removes an unnecessary delegating method when its implementation is as transparent as its name.
  • Replace Temp with Query: Replaces a local temporary variable with a query method.

Organizing Class Responsibilities

  • Extract Class: Splits an overloaded class by migrating related fields and operations into a separate class.
  • Inline Class: Merges a class that has ceased to provide distinct value back into another class.
  • Move Method / Move Field: Moves behavior or data to the class that consumes it most frequently.

Class Hierarchy Transformations

  • Pull Up Field / Method: Elevates common members shared across multiple subclasses to their shared superclass.
  • Push Down Field / Method: Relocates members needed by only specific subclasses downward.
  • Replace Conditional with Polymorphism: Removes switch(type) or if-else type inspections by leveraging dynamic method dispatch across subclass implementations.

Tip: Interactive 18 Refactoring Techniques Cheatsheet
Explore all 18 standard Fowler refactoring techniques with side-by-side Java code transformations and visual Mermaid UML diagrams in the Refactoring Lab.

Exercise 1
Question

Which refactoring technique replaces a switch on an object type code with polymorphic subclasses?

Show solution ↓
Solution

Replace Conditional with Polymorphism, which delegates execution to subclass overridden methods via dynamic dispatch.