liveBook · Manning

Refactoring

As explained in Simple Object-Oriented Design: Create clean, maintainable applications

Refactoring is the disciplined transformation of existing software structure without changing its externally observable behavior.

Refactoring is the disciplined improvement of existing software structure without changing what users observe. Its purpose is to keep complexity from becoming permanent: a design can begin awkwardly while the problem is still unclear, then be simplified once a better shape emerges. Done well, this makes changes faster, reduces regression risk, and lets more developers navigate critical code confidently. section 1.0 section 1.3.2

Work in small steps, but first get a helicopter view of the destination. Picture the resulting classes and their relationships; if that picture is unclear, sketch a lightweight UML-like diagram. This is planning, not a demand for a formal design process. section 2.1.4 section 8.2

Useful refactoring moves include:

  • Rename variables, methods, classes, and interfaces as their responsibilities become clearer. Names should reflect the domain language and make purpose, expectations, and results easier to grasp. section 2.2.1
  • Extract a dense decision into a named method, then introduce intermediate variables that explain its parts. section 2.2.4
  • Move a rule to the object that owns the relevant knowledge, so later changes have one clear home. section 2.2.4
  • Separate higher-level intent from lower-level implementation when complexity warrants it; keep infrastructure such as SQL or HTTP out of business logic. section 4.1.3

Making an email decision readable

An update-email condition initially combines opt-in preferences with checks for changed dates or descriptions in one hard-to-read if statement. Refactor it into shouldReceiveAnEmail(), name the two conditions, and let Offering answer isImportantInfoUpdated(). The sending code now reads as one decision, while the business rule is centralized in a place that can change independently. section 2.2.4

Listing 2.4 Deciding whether an employee should receive an update email
public void update(UpdatedOffering updatedOffering) {
 
  // ...
  // logic to update the offering
  // ...
 
  if(employee.wantsAnyEmailUpdates() ||
    (updatedOffering.isDateUpdated() ||
    updatedOffering.isDescriptioUpdated())) {    #1
 
    // send an update email to the employee
 
  }
}

Compare the original compound condition with the extracted, named decision to see how a small structural change exposes business intent. section 2.2.4

Refactor aggressively enough to prevent accumulating debt, not so aggressively that maintainable code receives needless attention. Prioritize code that changes, signals rising complexity, or is heading toward a design dead end. Stable code that rarely changes may not be urgent; waiting until a codebase is tangled can make refactoring infeasible. A redesign can be worth considering early, while change remains affordable. section 8.2 section 8.4

How Simple Object-Oriented Design: Create clean, maintainable applications covers it

How deep it goes
Explains planning, small-step practice, renaming, extraction, encapsulation, and redesign timing through a worked conditional example.
Examples
Uses a billing-system redesign and an employee update-email conditional rewritten into named decision methods.
What it assumes
Builds on object-oriented design while assuming familiarity with classes, methods, interfaces, and business-code structure.
What you can do after
Practice/perform incremental structural improvements that clarify code, localize rules, and address complexity before it becomes entrenched.
How it connects
Connects refactoring to object-oriented design, naming, ubiquitous language, encapsulation, dependency management, infrastructure, and business logic.

Also explained in

Compare how 22 books explain refactoring →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Simple Object-Oriented Design: Create clean, maintainable applications. The section links point to the parts of the book it draws on.