We recently tackled a beast: a 5,000-line utils.js file from 2018. It was a mess of spaghetti code, global variables, and side effects. The temptation was to ask ChatGPT to "rewrite this in TypeScript." We resisted. Here is why.
The "Chesterton's Fence" of Code
Legacy code is often ugly, but it works. It has survived years of production traffic, edge cases, and bug fixes. If you blindly rewrite it with AI, you lose that accumulated wisdom.
We used Vibe Code Detector to scan the file before touching a single line. The goal? To find the "Human Hotspots."
Finding the Pulse
The Vibe Check revealed three distinct types of code in the file:
- The Boilerplate (Vibe Score: 20/100): Generic helper functions like
formatDateanddeepClone. - The Business Logic (Vibe Score: 85/100): Complex pricing calculators with weird "magic numbers."
- The Glue (Vibe Score: 50/100): Functions that just passed data around.
Strategy: AI for the Boring, Human for the Critical
Once we mapped the file, we adopted a hybrid refactoring strategy.
Step 1: AI-ify the Boilerplate
We took the low-vibe helper functions and fed them to Claude. "Rewrite this in modern TypeScript with tests."
Result:
export const formatDate = (date: Date): string => {
return new Intl.DateTimeFormat('en-US').format(date);
};
Perfect. Clean, standard, tested. No human soul needed here.
Step 2: Preserve the Business Logic
Then we got to the pricing calculator. It had a comment: // Don't touch this or the Q3 audit will fail.
AI would have "cleaned" this function by removing the weird math. We kept it exactly as is, just adding type annotations. We preserved the "vibe"—the fear, the history, the specific business constraint.
Step 3: Documenting the "Why"
As we refactored, we added comments explaining why we kept certain ugly parts.
// LEGACY: This looks like a bug, but it accounts for the
// 2019 tax holiday logic. Do not refactor without Legal approval.
const calculateTax = (amount) => { ... }
The Result
We reduced the file size by 40%. We increased test coverage to 95%. But most importantly, we didn't break production.
By using Vibe Detection to identify which parts of the code were "generic" (safe for AI) and which were "human" (risky to touch), we de-risked the entire project.
Lesson: Don't just rewrite legacy code. Respect it. Find the human pulse within it, and build around it.