Neural Code Completion with Anonymized Variable Names

Nadezhda ChirkovaarXiv 2020

Abstract

Standard AI models for code often treat variable names as static tokens, similar to words in natural language. However, in programming, a variable's semantics are defined by its logical role and usage patterns rather than its identifier. This paper proposes a dynamic embedding mechanism that iteratively updates a variable's representation each time it is encountered in the source code. By utilizing a Dual-LSTM architecture, the model achieves renaming invariance, allowing it to maintain high performance even when variable names are completely anonymized. Experimental results on Python and JavaScript benchmarks demonstrate that dynamic embeddings significantly outperform traditional static models in tasks such as code completion and variable misuse detection.

Introduction

A fundamental challenge in applying Natural Language Processing (NLP) to source code is renaming invariance. Unlike natural language, where changing a character's name can alter the narrative context, renaming a variable in code (e.g., from counter to i) preserves the program's logic. Traditional models rely on "static embeddings," which assign fixed vectors to specific strings. This approach fails when encountering generic or obfuscated names, as the static vector carries no inherent logical information.

This research addresses this gap by defining a variable's semantics through its role within the program logic—such as "the variable storing a loop limit"—rather than its textual label.

Methodology

The proposed approach employs a recurrent mechanism to refine variable embeddings "on the fly" as the model processes the code.

AST Traversal and Representation

Source code is first parsed into an Abstract Syntax Tree (AST), which is then flattened into a sequence via depth-first traversal. Each element in the sequence is a (type, value) pair, providing both structural and lexical context.

The Dual-LSTM Architecture

The core of the system is a Dual-LSTM framework:

  • LSTM_main: Tracks the overall global state of the program.
  • LSTM_dyn: Updates the specific embedding of a variable whenever it appears. It takes the current program state and the variable's previous embedding to compute an updated representation.
Overview of the proposed approach: from source code to AST traversal and the Dual-LSTM architecture for dynamic embeddings.

Initialization Strategies

The model supports two initialization modes:

  1. Static Initialization: Vectors are based on the variable's name, leveraging descriptive identifiers when available.
  2. Constant Initialization: All variables start with the same vector, forcing the model to learn roles purely from context and ensuring true renaming invariance.

Datasets and Evaluation

The models were evaluated using the Python150k and JavaScript150k benchmarks. To prevent data leakage, the authors implemented a rigorous repository-level split, ensuring that entire libraries were excluded from training if any of their files appeared in the test set.

Results and Discussion

The dynamic embedding approach was tested on two primary tasks: Code Completion and Variable Misuse detection.

Performance on Anonymized Data

One of the most striking findings is the model's resilience to anonymization. In variable misuse tasks on Python, the dynamic model achieved 63.64% accuracy, whereas the standard static model's performance dropped to 25.17%. Furthermore, dynamic models trained on anonymized data often outperformed static models trained on full data, highlighting the importance of structural logic over naming conventions.

Performance comparison of static vs. dynamic embeddings on Python (PY) and JavaScript (JS) datasets.

Rare Value Prediction

Dynamic embeddings excel at predicting "rare" variables—those outside the top 1000 most frequent names. Since these variables are often the source of real-world bugs, the model's ability to handle them through contextual "slots" rather than memorized strings is a significant advancement.

Conclusion

Dynamic embeddings offer a more robust way to model source code by focusing on logical roles rather than identifier names. While currently implemented using LSTMs, the concept of updating representations based on usage context is highly relevant for future work with Transformer architectures and Graph Neural Networks. The authors suggest that performance on anonymized code should become a standard benchmark for evaluating a model's true understanding of programming logic.

Summary updated

BibTeX
@misc{chirkova2020neural,
  author = {Nadezhda Chirkova},
  title = {{Neural Code Completion with Anonymized Variable Names}},
  year = {2020},
  eprint = {2010.12693},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2010.12693},
}
Figure