DEV Community

Cover image for Refactor an Existing Codebase using Prompt Driven Development
Cynthia Zanoni for Microsoft Azure

Posted on

Refactor an Existing Codebase using Prompt Driven Development

This article is part of the Prompt Driven Development series from the VS Code YouTube channel. It is based on the video Refactor an Existing Codebase using Prompt Driven Development and explains the concepts, decisions, and workflows demonstrated during a real refactoring scenario using Visual Studio Code and GitHub Copilot.

🎥 Video Reference

Refactor an Existing Codebase using Prompt Driven Development


Refactoring an existing codebase is often more challenging than building something new. It requires understanding current behavior, preserving functionality, and improving structure without introducing regressions.

🏗️ Understanding the Refactoring Context

Before any prompting begins, the video emphasizes the importance of understanding the system as it exists today.

The demonstrated project is an inventory API built with:

  • Azure Functions using a flexible consumption plan
  • FastAPI with Python
  • Cosmos DB using the NoSQL API

The API exposes endpoints for managing products and categories, including batch operations. Before refactoring, the API is executed locally and exercised through its endpoints to confirm current behavior and establish a baseline.

This step ensures that refactoring goals are grounded in a clear understanding of functionality rather than assumptions.


🎯 Defining the Refactoring Goal

The refactoring goal is explicit and narrowly scoped.

The existing codebase contains a CRUD layer responsible for database interaction. However, this layer also includes:

  • Business logic
  • Data normalization
  • Validation
  • Exception handling

The objective is to separate responsibilities by introducing a service layer. After refactoring:

  • The CRUD layer should only handle direct database access
  • Business logic and helper functionality should live in dedicated service files

This clear separation of concerns becomes the foundation for all subsequent prompts.


📌 Five Key Learnings from the Video

1️⃣ Clear Intent Improves Prompt Quality

One of the strongest takeaways is that effective prompts start with clear intent.

Rather than issuing a vague instruction, the prompt file explicitly defines:

  • Which directories must be analyzed
  • Which files contain mixed responsibilities
  • What logic should remain in the CRUD layer
  • What logic should move to the service layer

This level of specificity enables the AI to reason about architectural changes rather than making superficial edits.


2️⃣ Prompt Files Enable Structured Refactoring

The video demonstrates the use of a prompt file to manage the refactoring task.

The prompt file:

  • Aggregates all relevant context
  • Defines goals and constraints
  • Acts as a reusable refactoring recipe

By running this prompt in agent mode, the AI can perform multi step operations such as reading files, creating new directories, moving logic, and updating references across the codebase.


3️⃣ Observability Is Essential During AI Assisted Refactoring

Refactoring is not treated as a passive operation.

Throughout the process, the developer actively monitors:

  • Task lists generated by the agent
  • Checkpoints indicating progress
  • Modified files and newly created directories
  • The Problems panel for errors and warnings

This reinforces an important principle: Prompt Driven Development assumes human oversight. AI accelerates the work, but the developer remains responsible for correctness.


4️⃣ Problems and Errors Are Part of the Workflow

As the refactor progresses, issues such as formatting errors, unused imports, and type warnings appear in the Problems panel.

Rather than fixing these manually, the AI is prompted to resolve them, using the Problems context as additional input. This iterative loop continues until blocking issues are resolved and the codebase reaches a stable state.

This demonstrates how tooling feedback can be incorporated directly into prompts.


5️⃣ Validation Completes the Refactoring Loop

Once structural changes are complete, validation begins.

The video shows:

  • Reviewing updated routes to confirm they now depend on services instead of CRUD logic
  • Running the API locally
  • Executing create, update, and delete operations
  • Verifying expected responses

This reinforces that AI assisted refactoring must meet the same quality bar as any other change. Testing and verification remain mandatory.


🛠️ Supporting Techniques Demonstrated

Several VS Code and Copilot features support this workflow:

  • Copilot Instructions to provide consistent context across prompts
  • Prompt Files to manage complex refactoring tasks
  • Agent Mode for multi step execution
  • Problems Context to guide iterative fixes
  • Model selection to evaluate different LLM behaviors

Together, these techniques enable a refactor that is structured, transparent, and repeatable.


Conclusion

This refactoring example shows that Prompt Driven Development can be effectively applied to existing codebases, not just greenfield projects.

By treating prompts as structured artifacts and maintaining continuous validation, developers can use AI to accelerate refactoring while preserving control, clarity, and code quality.

Prompt Driven Development does not replace engineering discipline. It reinforces it.

Top comments (0)