env.dev

AI Refactoring Prompts

Prompts for LLM-assisted refactoring. Extract functions, simplify logic, and improve code structure safely.

Overview

LLM-assisted refactoring can help you extract functions, simplify complex logic, improve naming, and restructure code while preserving behavior. Always ensure tests pass before and after refactoring.

Refactoring Prompt Template

Prompt
Refactor this code. PRESERVE ALL BEHAVIOR.

Goals:
- Extract reusable functions
- Reduce nesting (max 2 levels)
- Simplify conditionals (prefer early returns)
- Improve variable/function names
- Remove duplication

```typescript
[paste code here]
```

Show me the refactored code and explain each change.

Common Refactoring Tasks

  • Extract reusable functions from duplicated code
  • Convert callbacks to async/await
  • Replace nested conditionals with early returns
  • Improve variable and function names
  • Split large functions into smaller, focused ones
  • Convert class components to functional components with hooks

Pattern Migration

Describe the current pattern and target pattern (e.g. callbacks to async/await, classes to hooks), provide a sample, and ask the LLM to transform the code while explicitly preserving behavior. Always run tests after migration.

Frequently Asked Questions

How do I refactor safely with LLMs?

Always ensure tests pass before and after refactoring. Ask the LLM to preserve behavior explicitly. Review the diff carefully — LLMs sometimes change behavior while refactoring.

What refactoring tasks are LLMs best at?

Extracting functions, renaming for clarity, converting callback patterns to async/await, reducing nesting, and eliminating duplication. Less effective at large-scale architectural refactors.