Refactoring (Deep Workflow)
Refactoring changes structure, not behavior. Safety comes from small steps, fast feedback, and verification (tests, golden outputs, or controlled manual checks).
When to Offer This Workflow
Trigger conditions:
- - Code is hard to change; duplication; unclear module boundaries
- Need to prepare an area for a new feature without mixing behavior change
- Paying down tech debt with management expecting “no user-visible change”
Initial offer:
Use six stages: (1) clarify goal & scope, (2) establish safety net, (3) plan increments, (4) execute with reviewable commits, (5) verify behavior, (6) document & follow-ups). Confirm test coverage and release pressure.
Stage 1: Clarify Goal & Scope
Goal: Why refactor now—reduce coupling, enable feature X, remove dead code, improve naming.
Exit condition: Explicit non-goals (no feature changes in this effort unless separately scoped).
Stage 2: Establish Safety Net
Goal: Prefer characterization tests for legacy; golden outputs for data pipelines; use snapshot tests sparingly.
If tests are weak
- - Approval tests, short exploratory scripts, or pair review with domain expert
Stage 3: Plan Increments
Goal: Small commits, each leaving the codebase working (not necessarily perfect).
Practices
- - Move code, then change behavior in separate steps (Fowler-style when helpful)
- Separate mechanical renames from logic edits for reviewability
Stage 4: Execute With Reviewable Commits
Goal: Each PR/commit tells a story; avoid thousand-line “cleanup” dumps.
Stage 5: Verify Behavior
Goal: CI green; compare outputs for batch jobs; manual smoke on critical paths when needed.
Stage 6: Document & Follow-Ups
Goal: ADR or short module README for new boundaries; tickets for remaining debt accepted consciously.
Final Review Checklist
- - [ ] Scope and non-goals explicit
- [ ] Safety net matches risk
- [ ] Incremental, reviewable steps
- [ ] Behavior verified
- [ ] Follow-up debt tracked
Tips for Effective Guidance
- - Keep refactor commits separate from feature commits when possible.
- If behavior must change, it is not “pure refactoring”—plan as a migration with communication.
- Under hotfix pressure, minimize refactor scope or defer.
Handling Deviations
- - Strangler refactors: maintain adapters at boundaries until cutover is complete.
重构(深度工作流)
重构改变的是结构,而非行为。安全性来源于小步骤、快速反馈和验证(测试、黄金输出或受控的人工检查)。
何时提供此工作流
触发条件:
- - 代码难以修改;存在重复;模块边界不清晰
- 需要为某个新功能准备区域,且不混入行为变更
- 在管理层期望“无用户可见变更”的前提下偿还技术债务
初始提供:
使用六个阶段:(1)明确目标与范围,(2)建立安全网,(3)规划增量,(4)以可审查的提交执行,(5)验证行为,(6)文档与后续跟进。确认测试覆盖率和发布压力。
阶段1:明确目标与范围
目标: 现在为何重构——降低耦合、支持功能X、移除死代码、改进命名。
退出条件: 明确的非目标(除非单独划定范围,否则本次工作中不包含功能变更)。
阶段2:建立安全网
目标: 对遗留代码优先使用特征化测试;对数据管道使用黄金输出;谨慎使用快照测试。
如果测试薄弱
- - 批准测试、简短探索性脚本,或与领域专家进行结对审查
阶段3:规划增量
目标: 小提交,每个提交都使代码库可运行(不一定要完美)。
实践
- - 先移动代码,再在单独的步骤中更改行为(必要时采用Fowler风格)
- 将机械性重命名与逻辑编辑分开,以便于审查
阶段4:以可审查的提交执行
目标: 每个PR/提交都讲述一个故事;避免千行“清理”式转储。
阶段5:验证行为
目标: CI通过;对比批处理作业的输出;必要时对关键路径进行人工冒烟测试。
阶段6:文档与后续跟进
目标: 为新边界编写ADR或简短模块README;有意识地记录已接受的剩余债务工单。
最终审查清单
- - [ ] 范围和非目标明确
- [ ] 安全网与风险匹配
- [ ] 增量式、可审查的步骤
- [ ] 行为已验证
- [ ] 后续债务已跟踪
有效指导技巧
- - 尽可能将重构提交与功能提交分开。
- 如果行为必须改变,则不属于“纯重构”——应作为迁移计划并做好沟通。
- 在热修复压力下,最小化重构范围或推迟进行。
处理偏差
- - 扼杀者重构:在边界处维护适配器,直至切换完成。