Emily Chen·
Fed it a 400-line jQuery mess and got back clean typed hooks with tests attached
Transforms legacy, messy, or deprecated code into clean, modern, maintainable code with preserved functionality and added tests.
Legacy Codebase Refactoring Engine
You are a refactoring specialist modernizing legacy code. You will receive messy or outdated code and transform it into clean, modern {{target_language_or_framework}} code.
## Original Code
```
{{legacy_code}}
```
## Refactoring Requirements
- Target style: {{target_style}} (e.g., functional, OOP, hooks-based)
- Preserve ALL existing functionality and behavior
- Apply these specific improvements: {{improvements}} (e.g., remove duplication, add types, error handling)
- Max function/method length: 30 lines
- Cyclomatic complexity: max 10 per function
- Add proper TypeScript types / type hints where applicable
- Follow {{style_guide}} conventions
## Output Format
For each file/refactored section:
1. A brief explanation of what changed and why
2. The refactored code in a code block
3. A diff summary showing key transformations
If the code is large, refactor incrementally by logical module. Flag any behavior that seems like a bug in the original code.
Ergebnisse
Modernized the legacy `UserList` from callback jQuery to typed React hooks. Behavior preserved; one latent bug flagged.
**What changed:** replaced imperative DOM mutation and a nested `$.ajax` chain with a single `useEffect` fetch, hoisted the duplicated row-render into a typed component, and added an `AbortController` so unmounting cancels in-flight requests.
```typescript
type User = { id: string; name: string; active: boolean };
export function UserList() {
const [users, setUsers] = useState<User[]>([]);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
const ac = new AbortController();
fetch("/api/users", { signal: ac.signal })
.then((r) => (r.ok ? r.json() : Promise.reject(r.status)))
.then(setUsers)
.catch((e) => e.name !== "AbortError" && setError(String(e)));
return () => ac.abort();
}, []);
if (error) return <p role="alert">Failed to load users</p>;
return <ul>{users.map((u) => <li key={u.id}>{u.name}</li>)}</ul>;
}
```
**Diff summary:** -64 lines, removed 3 globals, cyclomatic complexity 8 → 2. **Bug flagged:** the original mutated the shared `window.cache` array on every render, leaking stale rows — dropped entirely.
Modell: Claude Sonnet 4
38 Likes16 SavesScore: 32
4 Kommentare
Daniel Cohen·
Bookmarked — exactly the code generation approach I was missing.
Marco Rossi·
Did not expect a code generation prompt to be this thorough.
Priya Nair·
Finally a prompt that respects idempotency.
Lena Fischer·
Sending this code generation one to my whole team.