## The Job Nobody Wants to Take
The Job Nobody Wants to Take
Every indie developer and small team has that one piece of code: the person who wrote it is long gone, the documentation is half a page, and the comments read "don't touch this, it will blow up." The business is still running, requirements keep piling up, but this legacy code is like an old house with no blueprints — it's livable as long as you don't touch it, but one move and you're afraid it'll collapse.
Last year I took over an order settlement module that had been running for six years — over twenty thousand lines, mixing three wildly different naming conventions, plus a piece of dead-loop protection logic that "nobody dared to delete." The team's initial estimate: three person-months, including a month reserved for regression testing. After hearing this, the boss said only one thing: "Can you make it faster?"
That's the pain point I want to talk about. It's not whether AI can write code, but rather: facing a pile of legacy code nobody understands, how much can AI compress the "understand—refactor—verify" pipeline?
What AI Can Actually Do Here
Let me give the conclusion first: AI can't take the blame for you, but it can take away nearly all the "grunt work."
First, it can quickly build a code map. I fed the entire module to the model and had it output a module dependency graph, external call sites, and the "suspected business meaning" of each code section. This step used to take a senior engineer a week to read through; AI produced a draft in forty minutes, and half a day of manual proofreading wrapped it up.
Second, it can identify safety boundaries. The scariest part of legacy code isn't making a wrong change — it's not knowing what your changes will affect. I had AI flag all the locations involving monetary calculations, permission checks, and external payment callbacks, and mark them as "refactoring no-go zones" — for these, we only did interface isolation, no internal rewrites. This step directly determined whether we could move freely later on.
Third, it can generate migration plans and test cases. Split into independently verifiable batches by module, with equivalent unit tests auto-generated for each batch. Run the old code once, run the new code once, and only merge when the results match.
Fourth, it can build a risk checklist for you at every step. It's not AI making decisions for you — it's AI listing out "the things you might not have thought of," and you check them off one by one.
My Actual Process
- Inventory (1 day): AI generates the code map + safety boundary checklist; humans confirm the no-go zones.
- Splitting (half day): Cut into 8 batches by dependency relationships, each batch no more than 1,000 lines.
- Refactoring (5 days): Have AI rewrite batch by batch, with interface signatures and business rules locked down in the prompts.
- Verification (3 days): Dual-run reconciliation of old vs. new, with AI assisting in analyzing difference logs.
- Launch (2 days): Grayscale traffic switching, with AI monitoring error differences.
Before and After: The Time Ledger and the Risk Ledger
| Phase | Traditional Estimate | Actual with AI | Savings |
|---|---|---|---|
| Code comprehension | 5 person-days | 0.5 person-days | ~90% |
| Rewriting/coding | 30 person-days | 5 person-days | ~83% |
| Test case writing | 15 person-days | 2 person-days | ~87% |
| Regression verification | 10 person-days | 3 person-days | ~70% |
| Total | ~60 person-days | ~11 person-days | ~80% |
Three months became two weeks. For a small team, this isn't just about saving time — it's about something that "could never fit into the schedule" becoming "doable this sprint." And the risk ledger matters just as much: the safety boundary checklist meant I touched the payment callback logic zero times during refactoring. The probability of things going wrong was structurally reduced, not left to luck.
To be honest though: AI isn't zero-cost. API call fees are pay-as-you-go (see the official pricing page for details), but compared to one person-month of salary, the ratio is negligible.
A Reusable Prompt Template
Apply the following directly, replacing the {} with your actual content:
你是一名资深后端工程师,负责遗留代码重构的安全评估。
【代码背景】
{粘贴模块说明 / 业务功能描述}
【任务】
请分析以下代码,输出:
1. 代码地图:模块职责、内部依赖关系、对外暴露的接口清单
2. 安全边界清单:标注所有涉及以下内容的代码位置(文件+行号),
并说明为何属于高风险:
- 金额计算与账务逻辑
- 权限校验与身份认证
- 外部支付 / 第三方回调
- 并发与锁、事务边界
3. 重构禁区建议:哪些代码只允许接口隔离、禁止内部重写
4. 分批重构方案:按依赖顺序拆分批次,每批给出验证标准
5. 迁移风险清单:列出至少5条可能被忽略的坑
【代码】
{粘贴代码}
【约束】
- 不得修改任何对外接口签名
- 不确定的地方明确标注"需人工确认",禁止猜测
- 输出格式使用Markdown表格便于勾选Three Lessons I Learned the Hard Way
- AI output must pass human review. Especially the parts it flags as "needs human confirmation" — skipping any single one could blow up during the grayscale rollout.
- Feed it only one batch at a time. With too long a context, the model's grasp of details degrades. Better to run more rounds.
- Dual-run reconciliation cannot be skipped. Old and new code process live traffic simultaneously (or replay historical data), and only a matching reconciliation counts as passing — AI can write the comparison scripts for you, but the process itself cannot be left to AI's judgment.
Final Thoughts
Legacy code refactoring used to be one of those things you "know you should do but never gets scheduled." AI compressed it from three months to two weeks — essentially by slashing the biggest fixed costs: comprehension cost and testing cost. For a team of only two or three people, this change isn't about efficiency gains; it's about making a whole category of work feasible for the first time.
If you haven't tried using large models for this kind of work yet, start by walking through the process I described today. Choosing a model service that's stable and fast enough will save you a lot of headaches — I've recently been using Thistoken's API platform, registration here: https://api.thistoken.ai/register . The entire pipeline from code analysis to test generation runs smoothly, with pay-as-you-go billing — perfect for validating with a small batch first before scaling up.
---
Ready to try it yourself? Sign up at https://api.thistoken.ai/register to get your API key and start building.
Bạn muốn thử Token.AI?
Tạo API Key cấp dự án, bật kênh trong bảng điều khiển và định cấu hình định tuyến, ngân sách và nhật ký kiểm tra.
注册 ThisToken.AI 并获取 API Key