The Problem That String Matching Can't Catch
Picture two final projects implementing Huffman compression. They produce identical output and share the same overall structure, yet a line-by-line comparison finds almost no matches. Variable names are different. Comments have been rewritten. One uses a HashMap and the other a TreeMap. The functions are in a different order.
A checker based on longest common subsequence over raw text will score a pair like this low. An experienced instructor reading both files can still see that they are the same solution, aggressively refactored.
Refactoring (renaming identifiers, splitting or merging functions, swapping data structures) is one of the most common tactics students use to hide copied code, and it works well against tools that only compare raw text. This article describes a two-stage similarity pipeline that holds up against it: token-based comparison to cast a wide net, and AST-based analysis to confirm structural matches.
Why Refactoring Defeats Naive Similarity Checks
Consider a simple Python function that computes a factorial:
def factorial(n):
if n <= 1:
return 1
else:
return n * factorial(n-1)
Another student, having seen the same logic, might submit this:
def compute_factorial(x):
if x == 0 or x == 1:
result = 1
else:
result = x * compute_factorial(x-1)
return result
A naive diff highlights different names, a different condition, and a new temporary variable, and reports only partial similarity. Structurally, though, the two functions are the same: a termination check at the top, a single recursive call, and isomorphic control flow.
Token-based detection normalizes identifiers (replacing each distinct name with a placeholder) and compares the sequence of token types, such as keywords, operators, and literals, instead of raw text. After normalization these two functions produce very similar token sequences. Token normalization alone still fails when the control flow itself changes, for example when recursion becomes iteration or a for loop becomes a while loop.
That is where AST structural similarity comes in. An abstract syntax tree captures the hierarchical structure of the code without concrete syntax details. Two trees can be compared with tree edit distance or with more scalable hashing techniques. Codequiry combines token-based fingerprints with AST comparison to cover the gaps that string methods leave open.
A Two-Stage Pipeline
A practical design runs in two stages:
- Stage one: token comparison. Run a token-based comparison on every submission, for example weekly. It is cheap enough to compare every pair and catches most direct copying and renaming.
- Stage two: AST verification. Only when stage one flags a suspicious pair or cluster, run AST-based analysis to check whether the structural similarity is high enough to warrant human review.
The split matters for performance. AST comparison is much more expensive per pair than token matching, so running it only on the top few percent of token-flagged candidates keeps the total cost manageable even in a course of several hundred students. You can build this from open-source tools or use a commercial code similarity checker that runs both passes for you.
What the second stage adds is reach. String matching catches lazy copies. Token matching catches renamed copies. The pairs that only the structural stage finds are the ones where a student put real effort into disguising the code, which are exactly the cases a text-based check misses.
String matching catches the lazy copies. The ones who put in effort to disguise their copying need a structural check.
The Trade-Offs: False Positives and Performance
No detection method is perfect. Token-based and AST-based approaches share two weaknesses: over-detection on boilerplate code and matches between students who independently implement the same algorithm.
Boilerplate (file headers, import statements, framework scaffolding) inflates similarity scores. Add a preprocessing step that strips standard library imports, instructor-provided starter code, and common constructs such as public static void main in Java before comparison.
Independent implementation is the harder problem. Two students can write a mergesort with nearly identical structure without ever seeing each other's code. Reduce these false positives by requiring that flagged pairs also share something distinctive: identical error-handling patterns, the same unusual variable names, or the same mistakes. Two students who both name a pointer runner_ptr in a linked-list problem is a stronger signal than two students who both use node.
A refactoring-resistant pipeline will still flag some pairs that manual review finds to be coincidental. That is acceptable, because manual review is already part of the process. The goal is to reduce the number of cases that need human judgment, not to replace it.
Lessons for Any Programming Course
- Don't rely on string comparison alone. If your tool only compares raw text, add token normalization at minimum, and an AST-based check for the pairs that matter.
- Treat boilerplate as noise. Build an exclusion list or use language-aware parsers to ignore common scaffolding, or harmless structural overlap will inflate every score.
- Combine similarity with anomaly signals. Refactored copies often leave other traces: identical error output, shared unusual names, or submission timestamps within a narrow window. Combining these with similarity scores improves precision.
- Budget time for setup. A home-built pipeline takes real time to set up and more to tune thresholds. Once calibrated, it can run largely unattended for a term.
For institutions without the time to build a custom pipeline, commercial tools like Codequiry provide these capabilities out of the box. Its code plagiarism detection algorithm includes both token-based and AST-based analysis, along with web-source checking to catch code copied from online repositories.
Frequently Asked Questions
Can refactoring-resistant detection catch code rewritten in a different language?
Only with extra work. Cross-language detection needs a language-independent representation, such as a control flow graph, rather than a language-specific AST. Most academic cases involve a single language, so same-language detection covers the bulk of the need.
How much does AST comparison slow down the process for large courses?
Exact tree edit distance is expensive, O(n^3) in the worst case. Practical tools use hash-based approximations of structure, and running the AST stage only on the top slice of token-flagged pairs keeps the added time modest even for large courses.
Should students be able to appeal a flag?
Yes, and that's a feature, not a bug. Let students submit a short explanation of their design and, where available, a trace of their development process such as git logs. Appeals catch the coincidental matches that any similarity pipeline will produce.