Stop AI from Copying Legacy Code: Purge Bad Samples First, Then Lock with Types and Tests
Western engineering teams using AI assistants face the same failure mode: models replicate whatever patterns dominate the repo, including workarounds and dead code. This approach gives a concrete, order-sensitive remediation path: purge the bad examples before tightening the gate, or the gate becomes another ignored rule.
AI coding models imitate what they see in context, and a codebase full of old APIs, temporary patches, and commented-out dead code becomes a training set for bad patterns. Rules in prompts or READMEs are soft constraints that models can ignore, while type errors and failing tests are hard rejections.
The article lays out a two-step governance order: first lower the density of bad examples by counting and deleting deprecated calls, patch markers, and duplicate blocks; then push constraints into code with enums, parameter objects, return types, and private encapsulation. Add tests that fail on regressions, run three layers of gates in CI, and write error messages that tell the model the replacement, not just a ban.
Order matters. If you enable strict linting before cleaning the repo, you'll face a wall of existing errors and end up adding ignore rules, which turn the gate into a reminder. Clean first so you can set a zero-tolerance threshold, then lock. A runnable Python script in the article scans for outdated calls, patch markers, and duplicate function bodies, exits 1 on any hit, and can be dropped into CI.
The article's core claim is that AI misbehavior is a sample distribution problem, not a model intelligence problem. That reframes code review: you are curating the curriculum the model reads.
The emphasis on order (purge before gate) is the sharpest point. Most teams jump to linting and then drown in ignore rules, which quietly converts a hard constraint back into a soft one.
The script's duplicate detection by function body fingerprint is deliberately simple, and the article admits its limits. That honesty strengthens the practical advice: use jscpd for real production checks.
The suggestion to delete commented-out code instead of keeping it as a record is counterintuitive to many developers, but it directly addresses the way models weight every token in context as evidence.