Pointing to Subwords for Generating Function Names in Source Code

Shogo Fujita, Hidetaka Kamigaito, Hiroya Takamura, Manabu Okumura

Abstract

Generating descriptive function names is a critical task for software maintainability, yet current deep learning models often struggle with "out-of-vocabulary" (OOV) terms. This paper proposes a novel approach that combines a hierarchical copy mechanism with a placeholder strategy for frequent subwords. By allowing the model to "point" to and copy specific subwords directly from the source code, the system significantly improves its ability to generate accurate names containing rare or domain-specific technical terms.

Introduction

In software engineering, a function name serves as a roadmap for developers. Inaccurate names, such as getTargetValue for a function that actually retrieves an index, can lead to significant technical debt and bugs. While existing models like code2seq have made strides in automating this process, they are limited by a fixed dictionary of common subwords (e.g., get, set, list). When encountering rare or domain-specific terms—like busybox—these models typically fail or output a generic UNK (unknown) token. Given that many function names are derived from words already present in the function body, there is a clear need for a mechanism that can "copy-paste" these subwords into the generated output.

Methods

The proposed approach builds upon the code2seq framework, which represents source code as an Abstract Syntax Tree (AST). The researchers introduced two primary innovations to address the OOV problem:

  1. Hierarchical Copy Mechanism: Unlike standard pointer-generator networks designed for linear text, this model employs a two-tier attention system tailored for tree structures. It first identifies the most relevant path within the AST and then selects the specific subword from that path's leaf nodes to be copied into the function name.
  2. Most Frequent Subword (MFS) Placeholder: Observations showed that the most frequent word in a code snippet appears in the function name over 31% of the time. The authors replace these frequent, potentially unknown words with a special MFS token. This allows the model to learn naming patterns associated with important local terms without needing them in the global vocabulary. The MFS token is swapped back for the original word during final generation.
The model architecture integrates an AST-based encoder with a hierarchical copy mechanism to handle structured code data.

Experimental Setup

The models were evaluated on two standard large-scale benchmarks derived from open-source GitHub projects:

  • Java-small: 691,974 functions.
  • Java-large: Over 15 million functions.

Performance was measured using F1 scores and a "modified F1" (F1**) designed to penalize models that repeat correct subwords to inflate scores.

Key Results and Insights

The hybrid model, which combines generation from a vocabulary with the hierarchical copy mechanism, outperformed baseline methods across several metrics:

  • Overall Performance: On the Java-small dataset, the model achieved a 4.86 point improvement in F1** score over the standard code2seq baseline (reaching 45.55 vs 40.69).
  • Handling Rare Terms: In tests targeting "Low-frequency" words (appearing in less than 0.0001% of the corpus), the baseline code2seq accuracy was near 0%, whereas the new model achieved 10.69%.
  • Structure Over Sequence: The results reinforce that treating code as a structured tree rather than raw text is essential. By applying the copy mechanism to AST paths, the model better captures program logic.
Comparative performance metrics (F1, F1**, and Accuracy) for the proposed model and baselines across small and large datasets.

Qualitative analysis further demonstrated the model's utility; for instance, where a baseline suggested the generic isAvailable, the proposed model correctly identified a rare term to generate isBusyboxAvailable.

Limitations and Future Work

Despite these gains, the authors identify two main areas for improvement. First, current evaluation metrics like F1 are rigid and do not account for synonyms (e.g., marking increase as wrong if the target is increment). Future work may involve metrics like METEOR to better handle semantic similarity. Second, the model currently only considers context within the function body. Integrating "global context," such as how a function is called elsewhere in the program, could provide further clues for accurate naming.

Summary updated

BibTeX
@inproceedings{fujita2020pointing,
  author = {Shogo Fujita and Hidetaka Kamigaito and Hiroya Takamura and Manabu Okumura},
  title = {{Pointing to Subwords for Generating Function Names in Source Code}},
  booktitle = {International Conference on Computational Linguistics},
  year = {2020},
  doi = {10.18653/v1/2020.coling-main.28},
}
Figure