Pattern 7: Human-in-the-Loop Refactoring

Continuous Improvement with AI Writing and Human Refactoring

Overview

Human-in-the-loop refactoring creates a cycle: AI generates code, you improve it, AI learns from your changes.

Why do this?

  • AI handles first pass using its API knowledge
  • Code matches your style and standards
  • You catch subtle bugs AI misses
  • You maintain control while leveraging speed
  • AI learns your preferences

This pattern maintains code quality in production systems. It combines AI’s pattern recognition with your business logic and taste.

Key Principles

  1. Iterative refinement - Each cycle improves
  2. Pattern teaching - Show AI your preferences
  3. Trust but verify - Review everything

Exercise: Legacy Migration

Migrate legacy code to modern patterns with AI.

Steps:

  1. AI pass: Have AI modernize a module. Don’t accept blindly.

  2. Review: Check logic changes, performance, style. Make corrections, explain why.

  3. Feedback: Show AI your corrections. Have it apply similar changes to the next module.

Your Turn

Pick your own project!

Examples:

  • Convert class React components to functional with hooks. Review state management and lifecycle handling.
  • Refactor synchronous Python to async with asyncio. Guide AI on race conditions.
  • Improve web accessibility. Have AI add ARIA roles, verify they make sense for screen readers.