Abstract
Recent debates in Natural Language Processing question whether large language models (LLMs) truly "understand" meaning or merely replicate statistical patterns of form. This study investigates this "meaning vs. form" gap in the context of programming languages, where semantics are formally defined. By applying metamorphic testing—semantically equivalent but syntactically "unnatural" code transformations—to models like CodeBERT and GraphCodeBERT, we demonstrate that these models capture robust semantic representations. Our findings suggest that Code LMs rely on underlying program logic rather than superficial frequency, even when faced with code absent from their training data.
Introduction
In NLP, the distinction between "meaning" and "form" is often blurred. However, computer code provides a precise laboratory for this inquiry: the meaning (semantics) of a program is defined by its execution. If Code Language Models (Code LMs) are merely "stochastic parrots," they should fail when code is rewritten in a logically equivalent but structurally unfamiliar way. This paper explores whether models like GraphCodeBERT truly grasp program logic or simply memorize common coding patterns.
Methodology: Metamorphic Testing of Code
To probe semantic understanding, we employed metamorphic testing, applying several meaning-preserving transformations to Java programs:
- Block Swap: The
thenandelseblocks of anifstatement are flipped while the condition is negated (e.g., changingif (a == b)toif (a != b)). - Operand Swap: The order of operands in a comparison is reversed (e.g., changing
a < btob > a). - Consistent Variable Renaming: Descriptive identifiers are replaced with uninformative names (e.g.,
referencetovar1) to test reliance on natural language cues. - Refactoring: Introducing intermediate boolean variables for conditions.
The models were evaluated using their primary pre-training task—Masked Language Modeling (MLM)—to predict "blanked out" operators in both original and transformed code.
Experimental Setup
We utilized the Java subset of the CodeSearchNet dataset. Transformations were applied exclusively to the test split to prevent data leakage. A brute-force search of the training corpus confirmed that the "unnatural" transformed versions were not present during training, ensuring the models had to rely on generalization rather than memorization.
Key Insights
Semantics over Frequency
Despite the transformed code having higher entropy (indicating it was "surprising" to the model), accuracy remained high. This suggests that models prioritize semantic consistency over the statistical frequency of specific syntactic patterns.
The Importance of Lookahead
Tokens appearing after a conditional operator were found to be more influential for predictions than preceding tokens. This mirrors human reasoning, where one must read the subsequent code blocks to infer the necessary initial condition.
Embedding Space Geometry
In the models' internal hidden layers, semantically equivalent programs (e.g., a block-swapped pair) were mapped closer together than programs that were syntactically similar but logically distinct. This indicates a mapping of diverse "forms" to a unified "meaning."
Results
GraphCodeBERT emerged as the most robust model, maintaining high accuracy even under significant structural changes.
As shown in the table above, GraphCodeBERT achieved 87.97% accuracy on original code and 68.34% on block-swapped versions. In the Operand Swap task, robustness was even higher, with models correctly predicting operators in over 90% of cases for both versions. Notably, a standard NLP model (RoBERTa) performed poorly, underscoring the necessity of code-specific pre-training.
Conclusion and Future Work
Our results indicate that Code LMs develop a sophisticated understanding of program semantics that transcends simple pattern matching. While this study focused on encoder-only models in Java, future research should extend these metamorphic diagnostics to large decoder-only models and a broader range of programming paradigms to further bridge the gap between computational semantics and human communicative intent.
Summary updated