Iterative AI Refactoring in Xcode: Copilot as a Prompt-Driven Loop
Loop Engineering is an emerging concept in AI-assisted software development that shifts the focus from crafting individual prompts to designing iterative AI workflows. This article explores how these principles can be applied with GitHub Copilot for Xcode, using an iterative prompt-driven workflow to repeatedly analyze, refine, and improve code until predefined quality goals are achieved and refactor a view controller in our Music Theory Learning app.
Depending on the tooling, this iterative loop is implemented at different levels. Frameworks such as LangGraph or the OpenAI Agents SDK explicitly model the control flow in application code, giving developers full control over orchestration, state management, and stopping conditions. Modern coding agents such as Claude Code or GitHub Copilot Agent also perform iterative execution, but much of the control loop is managed internally by the agent runtime. In these systems, prompts primarily define the objective, constraints, and success criteria, while the agent autonomously determines when to analyze, modify, verify, and continue iterating.
Screenshot of Xcode with GitHub Copilot for Xcode while modernizing a View Controller
GitHub Copilot for Xcode currently does not provide a fully autonomous agent runtime comparable to Claude Code or GitHub Copilot Agent. Nevertheless, many of the underlying principles of Loop Engineering can be applied by describing the entire optimization cycle directly within a single prompt. Instead of specifying only the refactoring task, the prompt also defines the iterative workflow, quality criteria, self-evaluation process, and stopping conditions.
For the example presented in this article, Copilot is instructed to analyze the existing `UIViewController`, perform incremental improvements, evaluate the result against a set of predefined quality goals, and repeat the process whenever those goals have not yet been met. The loop terminates only when all criteria are satisfied or a predefined maximum number of iterations has been reached.
This effectively creates a lightweight optimization loop within a single prompt. The developer defines the structure of the loop, while Copilot carries out the individual analysis, refactoring, and evaluation steps autonomously. Although this is not equivalent to a fully agentic workflow with external orchestration and tool execution, it demonstrates that the core ideas of Loop Engineering—iterative refinement, self-evaluation, and explicit stopping criteria—can already be applied with today's GitHub Copilot for Xcode.
Defining explicit stopping criteria is essential. Without clear limits, additional iterations may consume more tokens and execution time while producing only marginal improvements. Practical prompts therefore include constraints such as a maximum number of iterations, measurable quality objectives, or both. These safeguards keep the optimization process predictable while preserving the benefits of iterative refinement.
Modernizing an existing UIKit view controller into a SwiftUI view was successfully accomplished using the iterative prompt-driven workflow. The following prompt was used:
Refactor this ViewController autonomously.
Use the IntervalsView as implementation reference.
Continue iterating until all of the following are true:
- no compiler errors
- test for this ViewController pass
- no SwiftLint warnings
- the ViewController follows SOLID principles
- responsibilities are separated appropriately
- no duplicated code remains
After each iteration, evaluate your own work.
If any criterion is not met, continue improving.
Stop immediately when all criteria are satisfied
or when the maximum number of iterations 10 has
been reached, even if some criteria are still unmet.
Although a few manual adjustments to the generated code were necessary, the overall results were very good. The screenshots below show the original UIKit view controller on the left alongside the modernized SwiftUI implementation on the right.
|
|
Despite the current excitement, a few practical considerations are worth keeping in mind:
- Loop Engineering is not a formally defined term.
- Many "AI agents" are actually agentic workflows.
- Every additional loop increases complexity and maintenance effort.
- Debugging becomes harder because LLMs are non-deterministic.
- Prompts can gradually become part of the application's logic.
- More iterations mean higher costs and longer execution times.
- The quality of a loop is often difficult to measure objectively.
- Frameworks such as LangGraph simplify development but hide complexity.
- Architecture often matters more than the choice of model.
- Not every problem benefits from iterative loops.
- Avoid the temptation to solve every problem with an AI loop.
- Loop Engineering complements good software engineering, it does not replace it.
