liveBook · Manning

Refactoring

As explained in Software Testing with Generative AI

Refactoring is the restructuring of existing source code to improve its internal design without changing its externally observable behavior.

Refactoring restructures production code after its checks pass, improving design and testability while keeping the checks green. In a TDD loop, it follows the red and green stages before the cycle begins again. section 4.3

AI can assist by identifying risks that suggest where restructuring is needed, but developers must judge the findings against accuracy, consistency, requirements, and the existing checks. section 4.4.5 section 4.3

A practical refactoring loop is:

  • Create a failing unit check for the desired behavior. section 4.3
  • Add just enough production code to make the check pass. section 4.3
  • Restructure the code while keeping all checks green, then repeat the loop. section 4.3

Timesheet code

A Timesheet implementation stores project durations in a HashMap. Risk analysis highlights possible accuracy problems when submissions overwrite or accumulate existing durations and consistency problems involving project names. Those findings provide concrete targets for review and refactoring, rather than proof that a particular redesign is correct. section 4.4.5

Figure 4.1 The red/green/refactor TDD cycle

Use the red/green/refactor cycle to place refactoring after passing checks and before the next development iteration. section 4.3

How Software Testing with Generative AI covers it

How deep it goes
Explains the red/green/refactor sequence and risk-led review, but does not detail named refactoring transformations.
Examples
Uses a Java Timesheet implementation, AI-generated accuracy and consistency risks, and the red/green/refactor cycle.
What it assumes
Assumes familiarity with production code and checks, while introducing the red/green/refactor TDD sequence.
What you can do after
Apply it via TDD and unit-checking tools while using human judgment to verify AI-identified risks.
How it connects
Connects refactoring with TDD, unit checks, automated checking, code quality, and testability.

Also explained in

Compare how 22 books explain refactoring →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Software Testing with Generative AI. The section links point to the parts of the book it draws on.