场景 031 · Codex Use Case

重构代码库

以小步、可审阅的修改删除无用代码、拆解大文件、合并重复逻辑并更新旧模式。

EngineeringCode
Refactor your codebaseEngineeringCode

原文核对:2026-10-13 · 中文正文 · 英文提示词与演示

场景定位

以小步、可审阅的修改删除无用代码、拆解大文件、合并重复逻辑并更新旧模式。

难度
高级
时间跨度
1 小时

适合用于

  • 无用代码、超大模块、重复逻辑或过时抽象拖慢日常修改的代码库。
  • 希望原地现代化,而非迁移框架或技术栈的团队。

相关工具

Skill Creator

把有效的现代化模式、审查清单或一致性验证流程保存为技能。

Skill Creator ↗

起步提示词

英文原文 · 可复制后按实际材料修改

Modernize and refactor this codebase.

Requirements:
- Preserve behavior unless I explicitly ask for a functional change.
- Start by identifying dead code, duplicated paths, oversized modules, stale abstractions, and legacy patterns that are slowing changes down.
- For each proposed pass, name the current behavior, the structural improvement, and the validation check that should prove behavior stayed stable.
- Break the work into small reviewable refactor passes such as deleting dead code, simplifying control flow, extracting helpers, or replacing outdated patterns with the repo's current conventions.
- Keep public APIs stable unless a change is required by the refactor.
- Call out any framework migration, dependency upgrade, API change, or architecture move that should be split into a separate migration task.
- If the work is broad, propose the docs, specs, and parity checks we should create before implementation.

Propose a plan to do this.

简介

代码积累了无用路径、重复逻辑、旧抽象、大文件和遗留模式,导致每次修改成本过高时,可以通过重构降低工程债务,改善既有系统结构。

Codex 可先梳理混乱区域,再小步清理:删除无用路径、拆分模块、合并重复实现、更新框架模式,并为每轮加强验证。

原地改进的目标包括:

  1. 移除无用代码、旧辅助函数、废弃开关和不再需要的兼容层。
  2. 提取辅助函数、拆分组件,将副作用移到更清晰的边界,缩小嘈杂模块。
  3. 采用仓库当前约定:新框架基础能力、更明确类型、更简单状态流或标准库工具。
  4. 保持公开行为稳定,降低下次修改成本。

使用步骤

  1. 先梳理模块、重复逻辑、无用代码、测试、公开契约和过时模式。
  2. 每次只选一个主题:删除无用代码、简化控制流、更新旧模式或拆分大文件。
  3. 修改前说明当前行为、结构改进方案和能证明行为稳定的最小检查。
  4. 每轮审阅并运行最小有效检查,不把全部清理塞进一个 diff。
  5. 除非完成清理必需,否则将技术栈变化、依赖迁移和架构调整分为独立任务。

可以先用 Plan mode 制定重构计划。

使用 ExecPlans

代码现代化 cookbook 介绍了 ExecPlans:记录整体清理、预期终态和每轮验证。跨模块或多次会话时尤其有用;记录删除项、模式更新、稳定契约和暂缓事项。

把重复模式保存为技能

规则跨仓库、服务或团队反复使用时,可以采用框架专用技能,为高风险清理加入安全与 CI 技能,并把已验证的删除、提取模块或现代化清单封装成团队技能。

展开英文原文对照

Use Codex to remove dead code, untangle large files, collapse duplicated logic, and modernize stale patterns in small reviewable passes.

Introduction

When your codebase has accumulated unused code, duplicated logic, stale abstractions, large files, or legacy patterns that make every change more expensive than it should be, you should consider reducing the engineering debt with a refactor. Refactoring is about improving the shape of the existing system without turning it into a stack migration.

Codex is useful here because it can first map the messy area, then land the cleanup in small reviewable passes: deleting unused paths, untangling large modules, collapsing duplicate paths, modernizing old framework patterns, and tightening validation around each pass.

The goal is to improve the current codebase in place:

  1. Remove unused code, stale helpers, old flags, and compatibility shims that are no longer needed.
  2. Shrink noisy modules by extracting helpers, splitting components, or moving side effects to clearer boundaries.
  3. Replace legacy patterns with the repo’s current conventions: newer framework primitives, clearer types, simpler state flow, or standard library utilities.
  4. Keep public behavior stable while making the next change cheaper.

How to use

  1. Ask Codex to map the area before editing: noisy modules, duplicated logic, unused code, tests, public contracts, and any old patterns that the repo has outgrown.
  2. Pick one cleanup theme at a time: remove unused code, simplify control flow, modernize an outdated pattern, or split a large file into smaller owned pieces.
  3. Before Codex patches files, have it state the current behavior, the structural improvement it wants to make, and the smallest check that should prove behavior stayed stable.
  4. Review and run the smallest useful check after each pass instead of batching the whole cleanup into one diff.
  5. Keep stack changes, dependency migrations, and architecture moves as separate tasks unless they’re required to finish the cleanup.

You can use Plan mode to create a plan for the refactor before starting the work.

Leverage ExecPlans

The code modernization cookbook introduces ExecPlans: documents that let Codex keep an overview of the cleanup, spell out the intended end state, and log validation after each pass. They’re useful when the refactor spans more than one module or takes more than one session. Use them to record deletions, pattern updates, contracts that had to stay stable, and what’s still deferred.

Use skills for repeatable patterns

Skills are useful when the same cleanup rules repeat across repos, services, or teams. Use framework-specific skills when available, add security and CI skills around risky cleanups, and create a team skill when you have a proven checklist for unused-code removal, module extraction, or legacy-pattern modernization. If you end up doing the same modernization pass across more than one codebase, Codex can help turn the first successful pass into a reusable skill.