The refactor-first pattern cleans up or restructures legacy code before adding another layer, creating a replacement subsystem, or applying a migration pattern. It matters when legacy code is tightly coupled, poorly designed, or covered by incomplete refactoring attempts. section 12.4.2
A practical approach may flatten failed layers by inlining methods with automatic IDE refactorings, then sculpt and model the resulting logic. The work carries risk because the legacy system could break, but it can be safer than building another layer on unstable foundations. section 12.4.2
Use the pattern when:
- Legacy coupling prevents a clean bubble or replacement subsystem from being built on top. section 12.4.2
- Previous refactoring layers have accumulated without producing a coherent design. section 12.4.2
- The team can accept and manage the risk of changing legacy behavior before migration proceeds. section 12.4.2
Flattening failed refactorings
A developer attempting to create a clean bubble encountered a codebase containing several incomplete refactoring layers. The team flattened those layers and the underlying legacy by inlining methods, then reshaped and modeled the logic before proceeding. section 12.4.2
