A Simple Approach for Handling Out-of-Vocabulary Identifiers in Deep Learning for Source Code

Nadezhda Chirkova, Sergey Troshin

Abstract

Standard deep learning models for source code often struggle with "Out-of-Vocabulary" (OOV) identifiers—unique variable names that appear rarely in a dataset. This paper introduces a simple yet powerful preprocessing trick: instead of replacing rare names with a generic UNK token, they "anonymize" them using unique placeholders (like VAR1, VAR2). This preserves the structural logic of the code and significantly boosts performance in tasks like bug fixing and code completion without changing the underlying model architecture.

Introduction

In natural language processing (NLP), models typically operate on a finite vocabulary. However, source code presents a unique challenge: developers can create an infinite variety of identifier names. This results in a "long tail" distribution where a few keywords (e.g., if, while) are ubiquitous, but millions of identifiers appear only a handful of times.

The conventional approach involves maintaining a vocabulary of the top $N$ most frequent identifiers and mapping all others to a special UNK (unknown) token. In the context of code, this is highly destructive. For instance, the expression my_y = np.sin(my_x) + my_x might be represented as UNK = np.sin(UNK) + UNK if the variables are rare. This mapping destroys the identity of variables, making it impossible for a model to recognize that the second and third UNK tokens refer to the same entity, thereby obscuring the program's logic.

OOV Anonymization

To address this, we propose OOV Anonymization. The core concept is to replace rare identifiers with unique, reusable placeholders while retaining frequent identifiers in the vocabulary. Two primary strategies are explored:

  1. Ordered Anonymization: Rare identifiers are replaced by placeholders based on their order of appearance within a code snippet (e.g., the first rare identifier becomes VAR1, the second VAR2).
  2. Randomized Anonymization: Rare identifiers are mapped to placeholders selected randomly from a predefined pool.

This approach succeeds because placeholders allow the model to track repetition and identity. Even if the original name is unknown, the model learns that VAR1 appearing multiple times represents the same variable. Furthermore, because these placeholders appear across thousands of training examples, their embeddings become robust representations of "generic local variables," unlike specific rare names which suffer from sparse updates.

Experimental Setup

The methodology was evaluated on two large-scale benchmarks: Python150k and JavaScript150k. To ensure rigorous evaluation and prevent data leakage, a custom train-test split was implemented, organized by repository rather than individual files. This ensures the model is tested on entirely unseen projects.

Results and Analysis

The effectiveness of OOV anonymization was tested on two critical tasks: Variable Misuse (bug finding) and Code Completion.

Variable Misuse

In the variable misuse task, the model must determine if an incorrect variable has been used and identify the correct replacement. As shown in Figure 1, standard models fail significantly at low vocabulary sizes because the necessary context is lost to UNK tokens. In contrast, anonymized models maintain high accuracy (over 80%) even with minimal vocabularies.

Joint accuracy for variable misuse across different vocabulary sizes.

Code Completion

For code completion, measured by Mean Reciprocal Rank (MRR), anonymized models consistently outperformed both standard models and more complex "pointer mechanisms." Pointer mechanisms are a common architectural solution for OOV tokens, but they are harder to implement. Our results indicate that simple preprocessing can achieve superior results.

MRR for code completion on the JavaScript150k dataset.

Key insights from the results include:

  • Vocabulary Efficiency: Anonymization allows models with only 1,000 frequent tokens to outperform standard models using 50,000 tokens.
  • Structural Importance: The results suggest that for many code-related tasks, the structural usage of a variable is more informative than its specific name.

Conclusion and Future Work

OOV Anonymization provides a simple, architecture-agnostic way to handle the long tail of identifiers in source code. While effective, the method does discard semantic hints present in descriptive variable names (e.g., is_valid). Future research could explore hybrid approaches that combine anonymization with subtokenization techniques like Byte-Pair Encoding (BPE) to balance structural clarity with semantic richness.

Summary updated

BibTeX
@inproceedings{chirkova2021simple,
  author = {Nadezhda Chirkova and Sergey Troshin},
  title = {{A Simple Approach for Handling Out-of-Vocabulary Identifiers in Deep Learning for Source Code}},
  booktitle = {Conference of the North American Chapter of the Association for Computational Linguistics},
  year = {2021},
  doi = {10.18653/v1/2021.naacl-main.26},
}
Figure