Why Renamed Variables No Longer Hide Copied Code

Refactoring-resistant plagiarism detection is the part of similarity analysis that survives renaming, reformatting, and shuffling control flow. Under the hood it compares normalized token streams and abstract syntax trees instead of lines of text. On the same pair of files, a line diff told me 3%. A token pipeline told me 91%.

I know that pair of files because I spent three weeks in 2024 arguing about them. A payments client had hired two contract shops six weeks apart. Shop A delivered a refund module. Shop B delivered a "new" chargeback module. TypeScript, about 4,100 lines across both services. The internal reviewer signed off because GitHub's compare view showed almost no overlap. Then a QA engineer noticed the test function names rhymed.

Both shops billed full rate. One of them copied.

What refactoring-resistant plagiarism detection actually measures

Line-based tools treat source code like prose. That works when someone pastes a whole function, and almost never otherwise, because code has a tiny vocabulary. In a 40-line function, most lines are braces, keywords, and short expressions. The tokens that carry the intent are identifiers, and identifiers are the first thing anyone changes.

Here is the pair I pulled out of the payments audit, simplified to something readable. Function one, from shop A:

public static int maxInArray(int[] values) {
    int best = values[0];
    for (int i = 1; i < values.length; i++) {
        if (values[i] > best) {
            best = values[i];
        }
    }
    return best;
}

Function two, from shop B:

public static int peak(int[] data) {
    int top = data[0];
    for (int k = 1; k < data.length; k++) {
        if (data[k] > top) {
            top = data[k];
        }
    }
    return top;
}

Run a unified diff and you get two hunks of noise. Every identifier differs, so the matched lines are down to braces and keywords. That is your 3%. Normalize the identifiers into a single token class, strip comments and whitespace, and the two functions collapse into identical six-gram sequences. Same loop bound, same comparison direction, same post-loop return. That is your 91%.

Side-by-side code comparison in Codequiry showing a 91% match between two student submissions
Side-by-side comparison: Codequiry lines up matching code between two submissions, with confirmed and false-positive review labels.

Students find this trick in week two of an intro course, usually in a Reddit thread. Contractors find it because they are under a deadline and a rename pass takes ten minutes. The mechanics are the same either way.

Tokens, k-grams, and syntax trees, in plain terms

Every serious similarity tool does roughly the same three things, and the differences are in how they do them.

Lexing and normalization. The file is broken into language tokens, comments and formatting are discarded, and identifiers are often mapped to a type bucket, so best and top both become an integer variable. Some tools also normalize literal values so a shared table of constants does not create false hits.

Fingerprinting. MOSS, still the tool most departments reach for first, winnows token k-grams into a set of hashed fingerprints and looks for shared fingerprints across submissions. JPlag builds token sequences and runs greedy string tiling to find matched runs. Both approaches are genuinely resistant to renaming. This is not a case where every old tool is broken; MOSS earned its reputation in 1994 and still holds up on plain copy-paste.

Structural comparison. Tree-based tools such as Dolos and the engine inside Codequiry build an abstract syntax tree and compare subtree shapes. That catches reordering that token runs miss, like flipping an if/else or hoisting a nested loop into a helper function that is called from the same place.

Here is where I have to be honest about limits. Take the function above and rewrite it:

public static int peak(int[] data) {
    return java.util.Arrays.stream(data).max().getAsInt();
}

That version is mathematically identical and structurally unrelated. Every tool I have run, including the good ones, drops it to near zero. Aggressive rewrites into library calls genuinely destroy the signal, and no amount of cleverness in the matcher fixes that. It is worth saying out loud, because the vendor decks won't.

The thresholds nobody remembers to tune

Similarity scores are not facts. They are the output of parameters someone chose, and those parameters default to values tuned for small exercise-sized programs. JPlag's minimum match length is expressed in tokens, and the default sits low enough that a shared eight-line setup block from the course starter code registers as a hit. MOSS lets you point at a base directory so everyone's provided scaffolding gets ignored, and roughly nobody does it on the first run.

We shipped a fall semester with a config file still carrying the k-gram size we had picked during a summer pilot on 20-line exercises. On 400-line student projects it produced thirty-odd matches that were technically true and practically useless, and I spent a Saturday triaging them. The fix was two numbers. The cost was a week of a TA's time and some awkward email.

Similarity scores are the output of parameters someone chose. If you can't name the parameters, you don't have a finding, you have a hunch with a decimal point.

False positives cluster in three places: the instructor's own solution circulating in last year's section, textbook algorithms where everyone writes the same canonical loop, and course-provided libraries that students are supposed to share. Filter all three before you look at the queue. Every tool has a mechanism for it, and every tool's docs bury it under a flag description.

Where the AI problem overlaps with the copying problem

Large language models are pattern re-expressers. Ask for a bounding-box intersection check in Python and you get one of two or three shapes with fresh names, which is exactly the refactoring pattern that token normalization already handles. That means the same pipeline gives you a first pass on AI-written submissions without any new infrastructure.

It is not the same measurement, though. Peer similarity asks whether this file resembles another file in the cohort. AI detection asks whether the file looks statistically human: identifier entropy, comment-to-code rhythm, the distribution of low-probability token choices, whether every function has a docstring in the same voice. Those are different axes, and a submission can score low on one and high on the other. A student who rewrote a ChatGPT draft by hand lands in the middle of both and gets flagged by neither with any confidence.

Codequiry AI code detection report with average AI score, highest file score and a risk distribution
AI code detection: probability scores per file, flagging submissions likely written by ChatGPT, Copilot, Claude or Gemini.

Codequiry is one of the few platforms that runs both axes in a single check, alongside a web pass against GitHub and general web sources. That combination is what makes the review queue usable. A file that matches a peer at 78% and an AI score of 0.9 tells you something specific. Either signal alone tells you to go read 4,000 lines and guess.

What I'd actually run

The workflow I set up for the last two departments I worked with, and the one I use on contractor code, looks like this.

  1. Run the full cohort (or the full set of vendor deliverables) as one check, with starter code registered as a base so provided scaffolding is excluded.
  2. Compare peers, the open web, and GitHub in the same pass. Roughly a third of the copies I have caught were not from a classmate at all, they were from a repository with a tutorial README.
  3. Score for AI generation separately, then sort by whichever signal is higher.
  4. Read the top of the queue by hand. Similarity tools rank, they don't adjudicate. Anything above about 70% on a normalized token comparison deserves eyes; anything below 40% on a 300-line project is usually a shared idiom.
Codequiry web results tracing copied code to GitHub repositories and other web sources with per-domain scores
Web results: every domain a submission matched, scored per source, from GitHub repos to tutorial sites.

The piece that matters for scale is the API. Education teams want the dashboard, with the per-submission score rings and the review queue already sorted. Engineering orgs want a REST endpoint in the CI pipeline that fails a merge request when incoming vendor code matches something already in the repo at above 85%. Both of those are Codequiry solutions, and both beat the alternative I used for years, which was MOSS by email with a serial-number report URL that expired in two weeks.

If you want the head-to-head on that, I wrote up Codequiry vs MOSS separately. Short version: MOSS is a fine matcher with a 1990s interface and no web or AI pass. The matcher is not usually where the pain is.

The enterprise version nobody budgets for

The same question shows up in diligence. I have been on two acquisitions where the target's "proprietary" codebase turned out to contain chunks of a former contractor's earlier client work, and one where a claimed in-house library was a lightly renamed open-source project under AGPL. Neither was malicious. Both were expensive, because fixing them after close means rewriting modules under a schedule you no longer control.

The cheap version of that insurance is a source code plagiarism checker run against the vendor's deliverables and against public repositories before you sign, then again at each milestone. It takes an afternoon. It has saved clients of mine low six figures in rework, and it caught one contractor who had been quietly recycling the same billing service across four clients for two years.

Renaming variables was never a sophisticated disguise. It just happened that the tools most people had access to were comparing the wrong thing, and for a long time nobody in the room knew the difference between a 3% diff and a 91% match. That gap is closing, and the contractors and students who relied on it are finding out the hard way.

If you want to see what your own submissions look like under a normalized comparison, run a cohort through the code plagiarism checker and read the similarity report before you read the scores.