Evaluating Large Language Models for Code Changes: A Practical Guide to Avoiding Common Pitfalls
This article explains why a model that can generate a working demo may still fail on large-scale refactoring of an existing codebase. It shows how to turn a refactoring plan into a verifiable contract, recognize two common failure modes (externalized blame and shotgun debugging), apply layered acceptance checks, and govern the process with logs, change size, and rollbacks. It also recommends replacing subjective impressions with an internal benchmark when selecting coding models. The goal is stable performance under constraints, not impressive visuals.
1. Background: Why “the demo runs” does not mean “the codebase can be changed”
Asking a large language model to carry out a broad refactor of an existing project is a fundamentally different task from asking it to generate a demo page from scratch. The former requires the model to understand the existing code structure, follow project conventions, and converge gradually through a feedback loop of builds and tests. The latter is mostly about visual presentation and a single point of effect. Many teams are drawn in by impressive interfaces during evaluation, only to run into repeated failures during real engineering work.
A typical scenario: a project is built with a systems programming language, and the refactoring plan has already been broken down into what to do at each step and how to verify it. Given the same codebase and the same plan, several AI coding tools diverge sharply in performance. Some proactively clarify requirements and raise key questions. Some complete the plan in one pass and pass the tests. Others claim early on that they are done, then halt citing an “abnormal build cache,” fall into a state of inefficient random edits, produce a large number of meaningless changes, and are eventually abandoned.
This shows that evaluating a code model cannot rely on generation speed, interface appearance, or the wording of its chain of thought. What matters is its ability to keep taking correct actions under constraints.
2. Turn the plan into a verifiable contract
Before writing prompts, the first step in involving a model in a refactor is to turn the plan into an executable acceptance contract. Each step should contain four elements: inputs, scope of allowed changes, verification commands, and failure handling.
An illustrative plan structure (independent of any specific tool):
Step k:Goal: state clearly what behavior this step changesAllowed changes: list the modules/files that may be modifiedForbidden changes: interfaces and data structures that must not be touchedVerification: build + unit tests + key integration testsPass criteria: all pass with no new warningsFailure handling: roll back to the start of this step, output a minimal reproduction and assumptions
The value is that the moment the model deviates, you can judge it with an objective signal instead of arguing over “it says it is done.” In practice, plans that lack failure handling make it easier for the model to stack more edits on top of an already incorrect state.
3. Recognize two typical failure modes
The first is externalized blame. The model attributes a build failure to the cache, the environment, or package sources rather than suspecting its own code first. The test is simple: ask it for a minimal reproduction and an evidence chain. A genuine cache problem can usually be ruled out with one clean build and a minimal sample. If the model refuses to run the verification loop and only keeps asking for instructions, terminate that attempt immediately.
The second is shotgun debugging. It shows up as random edits in multiple places without locating the root cause; the number of changes balloons while the build still fails. The underlying problem is the absence of a hypothesis-driven debugging process. Hard rules can suppress it:
- Allow only one hypothesis and one change at a time;
- Write down the expected result and the verification command before making the change;
- After two consecutive verification failures, roll back and restate the hypothesis.
Illustrative pseudocode:
while not passed:state = rollback_to_last_green()hypothesis = model.propose_single_hypothesis(state)patch = model.apply_minimal_patch(hypothesis)result = run(compile + tests)if result.failed_twice:require_model_to_restate_problem_with_evidence()
4. Layered acceptance: turn “looks right” into “is right”
Complex refactors need layered acceptance to avoid the misjudgments that come from a single all-or-nothing verdict. Four layers are recommended:
- Syntax and type layer: build and static checks pass;
- Unit layer: core logic tests pass, with boundary values covered;
- Integration layer: contracts and data flow between modules are correct;
- Presentation layer: visual or interactive results are consistent with common sense and business constraints.
The presentation layer in particular calls for a common-sense check. When generating a small physics game, for example, abnormally large gaps between target objects, a scene that is nearly unrecognizable, or a requested style that is completely absent should all count as failures, not as “stylistic differences.” Also note that visual effects described in the model’s chain of thought do not count as delivery evidence; only results that run and can be observed do.
For reinforcement learning tasks, acceptance should focus on whether the training curves converge, whether the state and action spaces are aligned, and whether the rendering layer matches the logic layer. If the agent is abnormally sized, the terrain is not drawn, it crashes on launch, or the action layer is misaligned with the terrain layer, the result should be judged unacceptable even if the final numbers converge, because the deliverable cannot be used.
5. Process governance: logs, change size, and rollbacks
When using models in engineering work, process data is more reliable than conclusions. Collect at least:
- Time and number of verification runs per step;
- Lines changed and number of files touched;
- First-pass rate and number of rollbacks;
- Distribution of blame types (own code / dependencies / environment).
When change size balloons within a single step, or verification runs increase without the pass rate rising, trigger a circuit breaker: stop the current round, roll back to the last green state, and reduce task granularity. In practice, splitting a large refactor into more small steps is often more effective than switching models.
6. Selection advice: replace subjective impressions with a unified benchmark
Do not make decisions from scattered examples. Build an internal benchmark set: pick several reproducible tasks from real repositories, fix the plan, fix the verification commands, and fix the scoring criteria, then record first-pass rate, average rollbacks, human intervention time, and the proportion of change noise. Visual tasks and engineering-refactor tasks should be scored separately, so that a “stunning interface” does not mask “unreliable engineering.”
You can also run the evaluation infrastructure on general-purpose cloud capabilities, for example using a container platform to run isolated build environments, object storage to keep build artifacts and evaluation datasets, and a logging service with monitoring and alerting to track failure rates and time distributions. This makes evaluation sustainable, comparable, and auditable.
7. Conclusion
The maturity of large language models in code refactoring is not reflected in demo effects but in stability under constraints: whether they can proceed according to plan, locate problems with evidence, and roll back cleanly on failure. Writing the plan as a contract, layering acceptance, and quantifying the process are the key paths for developers to gain stable returns.
