liveBook · Manning

Refactoring

As explained in Practices of the Python Pro

Refactoring is the process of changing a software system's internal design without changing its externally observable behavior.

Refactoring changes a system’s internal structure so it is clearer, more maintainable, readable, performant, or flexible while preserving its existing outcome. It is especially useful when organically grown code has accumulated unused methods, oversized functions, or tangled responsibilities. section 1.4.2 section 4.3.2

Think of refactoring as rearranging a living room to fit how you use it now: the furniture changes, but the room remains the same room. In software, separating responsibilities makes faults easier to locate and future changes safer. section 1.4.2 section 3.3.1

Use small, deliberate refactorings as the code reveals where its design is working against you:

  • Plan before making larger changes, and notice opportunities to replace repeated calculations with reusable or precomputed results. section 1.7.1
  • Separate logical steps and concerns so each function, service, or module has a more focused responsibility. section 1.4.2 section 3.3.1
  • Prefer a suitable language feature or data structure over verbose hand-written logic when it makes the intent clearer. section 4.3.2
  • Reduce rigid conditionals by mapping choices to outcomes, and refactor toward flexibility before making a behavior change. section 7.1.2 section 7.2
  • Avoid premature abstraction: duplication can reveal real use cases, while the wrong abstraction can make future changes harder. section 4.3.2 section 7.1.1

From repeated calculation to a constant

A state-capitals script first extracts dictionary values and sorts them. If many parts of the program need the same result, wrap the calculation in a function; if the data is constant, compute it once and store US_CAPITALS. The refactoring shortens the code without sacrificing readability. section 1.7.1

Extracting behavior from a class

A Greeter class can call standalone day and part_of_day functions instead of owning methods that never use self. The Greeter then focuses on producing a greeting, while the extracted functions remain reusable elsewhere. section 3.3.1

Figure 1.4. What a thoughtfully planned e-commerce system might look like

Compare a tangled e-commerce workflow with a thoughtfully planned design that gives checkout steps clearer boundaries. section 1.4.2

How Practices of the Python Pro covers it

How deep it goes
Explains motivations, timing, decomposition, abstraction, and short Python transformations, but stops short of a comprehensive refactoring catalog.
Examples
Demonstrates state-capital extraction, Greeter restructuring, frequency counting, checkout decomposition, conditional mapping, and decoupled command messaging.
What you can do after
Apply it via Python by extracting responsibilities, replacing rigid conditionals, choosing clearer built-ins, and preserving existing behavior during change.
How it connects
Connects refactoring with software design, abstraction, encapsulation, decomposition, flexibility, maintainability, readability, and performance.

Also explained in

Compare how 22 books explain refactoring →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Practices of the Python Pro. The section links point to the parts of the book it draws on.