Skip to content
Code completion yields near-zero benefit in legacy projects; the problem is not the model

Code completion yields near-zero benefit in legacy projects; the problem is not the model

A common task: add a refund status field to an order service that has been running for seven or eight years. You open the file, and the same method has three versions—two commented out, one wrapped in a condition that is always false. You type self.refund_ on some line in the middle, and the completion popup shows three lines, calling methods from another payment gateway in the repository that has already been decommissioned.

You read it twice before confirming that it is a deprecated path, delete it, and write four lines yourself. This interaction repeats dozens of times a day, and the conclusion is clear.

The same model, in a TypeScript project that started only six months ago, often gets three lines right on the first try. The difference is not the model; it is the quality of the few hundred lines of context around the cursor. The benefit of code completion does not grow with project size; it grows with how clean the context is. Legacy projects happen to be where the context is dirtiest, so the benefit is close to zero, and in some files it is even negative.

Acceptance rate is a misleading metric

The acceptance rate reported by the editor has accepted characters as the numerator and displayed characters as the denominator. This metric is almost inevitably higher in old projects, because old projects contain more repeated boilerplate: field declarations, getters, log lines, error code mappings. The model suggests quickly, and you cannot be bothered to change it, so you just press Tab.

But boilerplate does not take time in the first place. Roughly estimating an afternoon of bug fixing: reading code and tracing call chains take up most of it, locating the problem takes a chunk, and the time actually typing on the keyboard may be only a little over ten percent. This is not a statistical conclusion; you can get roughly the same order of magnitude by logging a day’s operations every ten minutes. Completion optimizes that ten percent, and even if you cut it in half, the overall effect is only a few percentage points. That magnitude cannot support the expectation that “AI doubles efficiency.”

There is also an asymmetry: rejecting a suggestion is more expensive than accepting it. The suggestion is already displayed; accepting takes one Tab, while rejecting requires pressing Esc, reselecting, and checking again for leftovers. Under this cost structure, people tend to accept first and fix later, so the acceptance rate is pushed up, and the fixing part is not recorded in the metric.

More troublesome is that acceptance does not equal correctness. A suggestion that is changed after acceptance counts as a success in the metric, but in reality it is a rework. In old projects, this kind of rework does not surface immediately, because many errors only show up at runtime.

The dirtier the context, the more the suggestions look like guesses

Completion is essentially sampling the next segment of tokens given a prefix (now commonly also a suffix, fill-in-the-middle). Sampling relies on local statistical patterns, and legacy projects contaminate this pattern on several layers.

The first layer is the coexistence of multiple versions. A utility function has five similar implementations in the repository, four of which are old versions, and their combined line count is several times that of the new version. Retrieval or the context window votes by similarity and frequency, and what it votes out is dead code. The model is not doing anything wrong; it faithfully learned the majority style in this repository, and that majority style is wrong.

The second layer is the proportion of generated code. Protobuf and Thrift outputs, ORM migration files, frontend build artifacts, and multilingual string tables are highly repetitive and absolutely consistent in style, making them the model’s favorite context. They may account for more than half the lines, yet not a single character in them can be changed. Completion performs best in these files precisely because these files do not need humans to write them.

The third layer is uninferable naming. flag2, bizType, and doProcess in old code have semantics that exist only in someone’s head or on a Wiki that has long been unmaintained. The model can only guess literally; the call order it guesses looks reasonable but is wrong when run. The type system could have caught this at this layer, but old code heavily uses dictionaries, string keys, and reflection, so it cannot.

The fourth layer is dependency version mismatch. The repository locks a library version from three years ago, while the model knows the new API it saw during training. It gives client.request(...), while this version only has client.send(...). This kind of error is caught by a single compilation in a new project, but in an old project it drifts all the way to the test environment.

There is also a purely engineering reason: functions are too long. The context window is limited. If the few hundred lines near the cursor all fall inside a six-hundred-line giant method, the truly relevant information—such as where this variable is initialized or why this branch exists—may be two thousand lines away. What the model sees is a fragment.

flowchart TD
    A[补全弹出建议] --> B{用到仓库里没出现过的 API}
    B -->|是| C[删掉 自己写]
    B -->|否| D{建议超过五行}
    D -->|否| E[能一眼判断就接受]
    D -->|是| F{本文件风格一致}
    F -->|否| C
    F -->|是| G[对照相邻函数再决定]

This judgment order can be used directly. The core is the first rule: if a suggestion contains an API that has never appeared in this repository, delete it and rewrite it; do not try to modify it. The cost of fixing a wrong suggestion is usually higher than writing from scratch.

The slower the feedback, the more expensive the wrong suggestions

The net benefit of completion is roughly the typing time saved minus the time spent judging whether a suggestion is correct. In a new project, the second term is close to zero: the editor marks errors in red within a second, a type mismatch is a type mismatch, and tests finish in seconds. In an old project, the second term can be large.

To judge whether a suggestion is correct, you have to run several things through your head: Does this API exist? Does this field exist in this version? Does this logic conflict with the branch above? Is this style consistent with our conventions? Each one requires looking through code or searching the repository. After three rounds of searching, you could have finished writing it yourself.

So one criterion can be established here first: when the cost of judging a suggestion is higher than the cost of writing it yourself, completion is a net negative. The judgment cost roughly rises with two things: the number of lines in the suggestion, and how far the information it references is from the cursor. A multi-line suggestion over five lines that also references symbols not present in this file is basically a loss.

This also explains why “turning on completion makes things more tiring” in old projects is not an illusion. Writing code is a continuous state, and every popup is a context switch, and the cost of switching is especially high in files that require deep understanding. After a few interruptions, a piece of logic that needs to be thought through has to be picked up again.

For someone who has just taken over an old project, the cost is even higher. They are not yet able to judge whether a suggestion is correct. Seeing a syntactically fluent piece of code, they tend to think this is how the repository is written, learn from it, and copy the wrong style again. What completion does here is accelerate the spread of errors, not accelerate development.

Where it is actually useful in old projects

Saying the benefit is close to zero does not mean zero. There are several kinds of work that are “greenfield” in legacy projects, where completion works almost as well as in new projects.

The most obvious is test code. The old logic under test can be very dirty, but the test file itself is new, consistent in style, clear in dependencies, and only a few dozen lines per file. Adding tests to old code is exactly what legacy projects should do most, and completion is useful for this.

The second kind is one-off scripts: data migration, log cleaning, reconciliation, batch fixes. This code does not go into the main branch, does not need to cater to the repository style, and is deleted after running. The model’s generic style becomes an advantage here.

The third kind is new modules. As long as the new directory is not mixed with old code and retrieval does not pull old code in, the effect is close to that in a new project. This places engineering requirements: directory boundaries must really be separated, and new code must not be stuffed back under the old package’s utils/.

The fourth kind is structured text: SQL, YAML, regular expressions, log parsing expressions. The correct way to write these languages is highly consistent worldwide and is not affected by this repository’s style.

Putting these together, the pattern is clear: the benefit does not depend on the age of the project, but on the local consistency of the two hundred lines around the cursor. In a repository that started ten years ago, the newly created directory works just as well.

Directory type Suggested strategy Reason
New modules, test directories Fully on, including multi-line Locally consistent, fast feedback
One-off script directories Fully on Not merged into mainline, high tolerance for errors
Config, SQL, YAML Fully on Globally uniform syntax
Core legacy services Single-line only or off Multiple versions coexist, high cost of judgment
Generated code directories Off Should never be hand-edited anyway
Giant files over a thousand lines Off Context gets truncated

How to measure it once, don’t go by feel

Most debates about completion stop at the level of gut feeling. To judge whether it’s worth turning on, you have to change the metrics.

Acceptance rate cannot be used as a decision metric; it rewards boilerplate code. Two substitutable metrics are: the number of review reworks per unit of change, and the time from starting work to merge. The former reflects suggestion quality, the latter reflects overall efficiency. Don’t group by project; group by the “dirtiness” of directories: the number of near-duplicate implementations in the same directory, whether there is test coverage, and the average function length. These three can be computed by script in most repositories, which is more reliable than manual judgment.

A lightweight approach is to record, over one week, the number of times each “deleted suggestion” and “kept suggestion” occurs, aggregated by directory. For directories with a keep rate below one-fifth, just turn off multi-line completion. This rule has a small sample size, so use it only to decide on/off, not to draw conclusions about “how much efficiency was gained”—the latter requires a much larger sample size.

The configuration looks roughly like this, graded by path rather than a global one-size-fits-all switch:

YAML11 lines
1
2
3
4
5
6
7
8
9
10
11
completion:
default: single_line
overrides:
- path: "src/new/**"
mode: multi_line
- path: "tests/**"
mode: multi_line
- path: "src/legacy/billing/**"
mode: "off"
- path: "**/*_pb2.py"
mode: "off"

A stop rule you can use right away

If you delete completion suggestions in the same file three times in a row, turn off multi-line completion in that directory and keep only single-line. Three times is a very low threshold, but it’s enough to show that the file’s context and the model’s understanding don’t match; the loss after turning it off is small, because single-line completion is still there, and variable names and API names can still be completed.

Second rule: if suggestions that were accepted and then changed by you exceed half in a directory, it means most of the writing style in that directory is itself wrong, and completion is amplifying it. At this point what you should do is clean up dead code, unify style, and add type annotations, not switch models or tweak prompts.

One last judgment call. Poor completion performance in old projects is not a model capability problem, it’s a context governance problem. Dirty context is the repository’s historical debt, and the model merely holds up this debt to the light. If you don’t plan to pay off the debt, turning it off in these directories is less trouble than any tuning.

About this Post

This post is written by Lu's Blog, licensed under CC BY-NC 4.0.

#Code Completion #Engineering Efficiency #Legacy Systems

Comments