用AI重构遗留代码

一个安全的、循序渐进的工作流程,使用AI改进现有代码而不破坏有效的部分。

3 steps
codingdeveloper-toolsproductivity
1

在修改之前理解代码

粘贴代码,让AI解释它的功能、有哪些依赖关系以及如果更改它可能会破坏什么。

Example prompt
Explain what this code does in plain English. Identify: the purpose of each function, any side effects, external dependencies, what other parts of the codebase likely depend on this, and what the riskiest parts to change are. Code:

[PASTE CODE]
2

获得优先级重构计划

让AI按影响和风险列出重构改进。首先从低风险、高价值的更改开始。

Example prompt
Suggest refactoring improvements for this code, ordered by: impact (how much it improves readability/performance/maintainability) and risk (how likely it is to introduce bugs). For each suggestion: what to change, why it's better, and any risks. Code:

[PASTE CODE]
3

每次重构一个函数并测试

给AI一个函数来重构。每次更改后,运行您的测试套件。永远不要在单次AI交互中重构多个函数。

Example prompt
Refactor only this single function. Preserve the existing behavior exactly — do not change what it returns or its side effects. Apply these improvements: [LIST IMPROVEMENTS FROM PREVIOUS STEP]. Show the before and after, and explain each change. Function:

[PASTE SINGLE FUNCTION]
Ready to try Emergent?
Follow this playbook with the actual tool
View Tool