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

Nadezhda Chirkova, Sergey Troshin

Abstract

There is an emerging interest in the application of natural language processing models to source code processing tasks. One of the major problems in applying deep learning to software engineering is that source code often contains a lot of rare identifiers, resulting in huge vocabularies. We propose a simple, yet effective method, based on identifier anonymization, to handle out-of-vocabulary (OOV) identifiers. Our method can be treated as a preprocessing step and, therefore, allows for easy implementation. We show that the proposed OOV anonymization method significantly improves the performance of the Transformer in two code processing tasks: code completion and bug fixing.

Introduction

In the burgeoning field of Deep Learning for Source Code (SCP), researchers frequently adapt natural language processing (NLP) architectures to software engineering tasks. However, source code presents a unique challenge: the "long tail" of identifiers. Unlike natural languages with relatively stable lexicons, programming languages allow for the infinite creation of new variable and function names. In typical code datasets, fewer than 1% of identifiers appear more than five times. This sparsity creates an "Out-of-Vocabulary" (OOV) problem that standard NLP techniques struggle to resolve.

The OOV Challenge in Source Code

When training models on code, developers must decide how to handle variable names. Maintaining a vocabulary of every unique identifier leads to millions of entries, resulting in models that are prohibitively slow and memory-intensive. The standard workaround involves keeping the top 10,000–50,000 most frequent names and replacing all others with a generic UNK (unknown) token.

In code, this approach is often disastrous. If a function utilizes three distinct rare variables—for example, my_x, my_y, and my_z—the model perceives them all as identical UNK tokens. This erasure of identity destroys the logical flow and structural relationships within the code. While sub-tokenization methods like Byte-Pair Encoding (BPE) can mitigate this, they significantly lengthen sequences and can break alignment with the code's Abstract Syntax Tree (AST), complicating the use of structure-aware models.

Proposed Method: Identifier Anonymization

Chirkova and Troshin propose OOV Anonymization as a simple preprocessing step. Instead of collapsing rare identifiers into a single UNK token, they replace them with unique, numbered placeholders. This preserves the identity of variables without requiring a massive vocabulary.

Two primary strategies were explored:

  1. Ordered Anonymization: Rare identifiers are replaced based on their order of appearance in a snippet (e.g., the first rare identifier becomes VAR1, the second VAR2). Subsequent occurrences of the same identifier use the same placeholder.
  2. Randomized Anonymization: Placeholders are selected randomly from a fixed pool for each code snippet.

Crucially, frequent identifiers (such as standard library functions like print or sin) are left untouched. This allows the model to retain the semantic meaning of common operations while using placeholders to track the flow of user-defined variables. Because this is a text-processing trick, it requires no architectural changes to the underlying Transformer model.

Experimental Results

The researchers evaluated their approach on the Python150k and JavaScript150k datasets using a custom train-test split to prevent data leakage from shared repositories.

Variable Misuse (Bug Fixing)

In the Variable Misuse task—identifying and fixing bugs where the wrong variable is used—the anonymization method showed dramatic improvements. As shown in Figure 1, the anonymized model achieved approximately 87% joint accuracy. In contrast, the standard model using UNK tokens struggled to reach 81%, even with a significantly larger vocabulary. Notably, the anonymized model maintained high performance even when the vocabulary size was reduced to just 1,000 frequent words.

Joint accuracy for the Variable Misuse task comparing anonymization strategies against a standard baseline.

Code Completion

For the code completion task, the proposed method was compared against the "Pointer Transformer," a more complex architecture designed specifically to handle rare tokens by "copying" them from the input. The results in Figure 2 demonstrate that OOV anonymization consistently outperforms the Pointer baseline across both Python and JavaScript datasets. By helping the encoder distinguish between rare variables from the first layer, anonymization provides a more robust representation of the code's logic than pointer mechanisms.

Mean Reciprocal Rank (MRR) for code completion on Python and JavaScript datasets, showing OOV anonymization outperforming Pointer and Standard models.

Discussion and Conclusion

The success of OOV anonymization stems from its ability to preserve the "shape" of an algorithm. Because placeholders like VAR1 appear thousands of times across the training set, the model learns high-quality, "prototypical" embeddings for them, effectively creating a workspace for generic variables.

While simple, the method requires care; for instance, in ordered anonymization, models might learn to predict VAR2 simply because VAR1 was just seen. The authors mitigated this by masking the first occurrence of anonymized values during training. Overall, OOV anonymization offers a highly effective, easy-to-implement solution for the long-tail problem in source code processing, allowing standard Transformers to achieve state-of-the-art results without added complexity.

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