Refactoring 300K Lines of Code in Six Months: Lessons from AI Coding Tools
A senior engineer recounts a six-month solo rewrite of a legacy internal web app (over 300K lines including tests and config). The piece covers task decomposition, context management, generate-review-verify loops, tests as a safety net, real costs beyond the model bill, and sustainable practices for teams adopting AI-assisted development.
Refactoring 300K Lines of Code in Six Months: Lessons from AI Coding Tools
1. Background: A larger-than-expected rewrite
I am a software engineer with seven years of experience, mostly on production web applications. Since large language models became practical, I have treated AI as an amplifier rather than a threat: it can speed delivery, produce demos quickly, and sit inside products for complex reasoning. That led me to an informal “AI applications advisor” role, helping the team judge model limits and where to apply them.
This project started with an internal web app that had been in use for years. The first version was written by an intern, forked from another intern project I had written seven years earlier. Technical debt stacked up: almost no automated tests, tight coupling, and a messy build. To get management buy-in, I promised to redesign and reimplement about 100K lines of core code in a few months on my own. The actual output, including tests and configuration, exceeded 300K lines.
In hindsight the timeline was too aggressive. What follows is a full retrospective of pushing AI coding tools to their limit over six months.
2. Engineering the human–model loop
2.1 Split requirements into independently verifiable tasks
Large models are weak at absorbing huge, fuzzy requirements in one shot. My approach:
- Split the system by business domain: auth, permissions, import/export, reporting, notifications, and so on.
- Split each domain into units that could be finished in an afternoon.
- Attach a clear acceptance checklist, written as a structured brief, before handing work to the model.
Illustrative task template:
Task: Refactor the user import moduleScope: src/import only; do not touch other modulesInput: CSV / Excel files, up to 200MBOutput: Normalized list of domain objectsAcceptance:1. Invalid rows are logged with details and skipped2. Duplicate primary keys keep the latest record3. Unit tests cover at least 85% of branchesConstraint: No new third-party dependencies
That granularity made model output easy to evaluate and kept review cost under control.
2.2 Context management is the real bottleneck
After long use, quality depends less on how “smart” the model is and more on how clean the context you feed it is:
- Keep an architecture contract. Layering rules, naming, error handling, directory layout. Inject it at the start of every session to cut style drift.
- Divide and conquer; do not dump the whole repo. Focus the model on one module or file group so unrelated code does not dilute attention.
- Reuse prompt templates. Examples: “generate unit tests,” “types from an API spec,” “rewrite imperative code as declarative.” Lock output format and boundary conditions in the template.
- Roll a conversation summary. In long chats, periodically have the model recap confirmed decisions, then start a new session so it does not forget or contradict itself.
2.3 Generation rhythm: generate, review, verify
I used a fixed loop:
Write the task brief → model produces a patch → static checks → unit tests → human review → merge
Human review is not optional. Typical issues in generated code: missed edge cases, missing concurrency safety, outdated assumptions about framework APIs, and hallucinated calls that look correct. Anything involving security, transactions, concurrency, or data migration was reviewed line by line. CRUD and boilerplate could be spot-checked.
2.4 Automated tests as a safety net
In a high-risk rewrite, tests are not optional:
- Write characterization tests on the old system’s critical paths first, to lock current behavior.
- Keep the suite green throughout; any behavior change must be explicit.
- Let the model draft tests, but I own the data and assertions.
- Hook CI so every merge runs static analysis, unit tests, and a build.
That net is what made a multi-month, large-scale change possible without losing control.
3. A realistic cost–benefit ledger
A high-end model subscription cost about 1,400 RMB per month, nearly 10,000 RMB over six months. After the project, a large amount of manual work was automated; time saved for the company far exceeded that spend. Cost is not only the invoice:
- Time: Writing prompts, reviewing output, and correcting drift meant combined human-plus-model hours were not much lower than a traditional approach.
- Cognition: Constantly switching between trusting the model and staying skeptical is exhausting.
- Health: Long nights to hit the deadline wrecked sleep. I joked that it might cost “five years of life,” but the occupational-health warning is real.
4. Health and sustainability: the constraint people skip
Intense AI-assisted work creates an illusion: if code appears faster, people can accelerate without limit. In practice the bottleneck moves from typing to decision and review speed, and decision fatigue is real. Lessons:
- Hard work windows: No more than six focused development hours per day; the rest for communication and lighter work.
- Forced recovery: At least one full day per week with no code.
- Avoid the “one more generation” trap: When the model keeps missing, stopping often beats retrying.
- Standing desk and scheduled movement: Physical wear from long, intense sessions is also irreversible.
If I did it again, I would pad the schedule by at least 50% and get at least one peer to share review.
5. Advice for team adoption
- Standards before models. AI amplifies existing discipline—and existing chaos.
- Tiered trust. Match review intensity to code risk.
- Treat prompts and contracts as team assets, not personal notes.
- Measure real efficiency: cycle time, defect rate, and rework before vs. after—not gut feel.
- Watch people. When you roll out AI coding, also set a sustainable work-rhythm policy.
6. Closing
Six months, 300K lines, about 1,400 RMB a month: those numbers sit on top of an experiment at the edge of what one person plus tools can do. AI coding tools can turn one engineer into a small team. They do not replace architectural judgment, risk awareness, or a commitment to quality, and they do not excuse burning out. The right posture is not “burn yourself faster.” It is amplify capability with engineering methods, and protect yourself with a sustainable pace. That is the lesson worth keeping from this extreme experiment.
