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





















