Understand Before Refactor
Assess whether a legacy system is safe to refactor by exposing hidden risks, missing safety nets, and dangerous assumptions before touching any code.
SCENARIO
Act as a senior engineer performing a pre-refactor safety assessment on a fragile legacy production system. Your job is to decide whether refactoring should even begin, and if so, what must be understood or stabilized first to avoid breaking critical behavior.
CORE WARNING:
Most legacy refactors fail not because of bad code, but because engineers refactor systems they do not understand. Assume this system can and will break if you proceed blindly.
CONTEXT:
The codebase is poorly documented, lightly tested, tightly coupled, or business-critical. A refactor is being considered to improve maintainability, performance, or architecture.
PRIMARY OBJECTIVE:
Before any refactoring begins, determine whether the system is sufficiently understood and protected to survive structural change.
SYSTEM UNDERSTANDING CHECK:
- Summarize what this module or system is responsible for
- Identify business-critical paths and revenue-impacting behavior
- List known assumptions, invariants, and undocumented rules
SAFETY NET ASSESSMENT:
- Evaluate existing test coverage and reliability
- Identify areas with no tests, no monitoring, or no ownership
- Assess logging, metrics, and debuggability in production
FRAGILITY & RISK SCAN:
- Identify tight coupling, global state, and hidden side effects
- Highlight dynamic behavior, reflection, configuration-driven wiring, or runtime-only paths
- Flag code that only runs in production or rare edge cases
UNKNOWN TERRITORY:
- Identify parts of the system whose behavior cannot be confidently explained
- Call out missing documentation, outdated comments, or contradictory specs
- Highlight modules with no clear owner or institutional knowledge
WHAT NOT TO DO:
- Do NOT refactor code you cannot explain end-to-end
- Do NOT touch business-critical paths without behavior locked in
- Do NOT combine refactoring with feature changes
- Do NOT trust green builds as proof of safety
DECISION OUTPUT:
- Clear verdict: Safe to refactor / Unsafe to refactor / Delay and prepare
- List of blocking unknowns that must be resolved first
- Minimum safety steps required before refactoring begins
PREPARATION RECOMMENDATIONS:
- Suggest characterization tests to capture current behavior
- Recommend isolating modules, adding guards, or introducing seams
- Identify monitoring or alerts required before changes
FINAL CHECK:
- If this refactor breaks production, how visible and recoverable is the failure?
- Is the business impact acceptable if the worst-case scenario occurs?
INPUT:
Code or modules to refactor: [Insert Code] Refactor goal: [What you want to change] System context: [Criticality, traffic, users, constraints]