Learning to Recommend Method Names with Global Context

Fang Liu, Ge Li, Zhiyi Fu, Shuai Lu, Yiyang Hao, Zhi Jin

Abstract

Method naming is a fundamental challenge in software development, directly impacting code readability and maintainability. Traditional automated recommendation tools are often "nearsighted," focusing exclusively on the local method body. This paper introduces the Global Transformer-based Neural Model (GTNM), which incorporates global project context—such as documentation and cross-file method signatures—to improve recommendation accuracy. Our evaluation shows that GTNM significantly outperforms existing state-of-the-art models, achieving a 62.01% exact-match accuracy.

Introduction

In software engineering, clear method names like getMaximumResourceCapability are essential for understanding code intent. However, as projects grow, maintaining consistent and descriptive names becomes difficult. Existing tools like code2vec and DeepName primarily analyze local context (the code within the method body).

Statistical analysis reveals a significant limitation of this approach: for approximately 10.87% of methods, the tokens required for a correct name do not exist within the method body at all. Instead, these tokens often appear in the names of other methods within the same project or in associated documentation. To overcome this "nearsightedness," a model must be able to efficiently digest global information without being overwhelmed by noise.

Methodology: The GTNM Architecture

The proposed GTNM model is a Transformer-based sequence-to-sequence architecture designed to process three levels of information: local context, project-specific context (in-file and cross-file method names), and documentation context (Javadoc).

Figure 1: The GTNM architecture featuring dual encoders and invoked weighting for global context processing.

Key Innovations

  • Dual Encoders: GTNM employs a standard Code Encoder for general inputs alongside a specialized Project Context Encoder to handle the broader project scope.
  • Invoked Weighting: To prioritize relevant information, the model uses a mask vector to assign higher attention weights to methods actually invoked by the target method. This ensures that semantically related methods have a greater influence on the generated name.
  • Transformer-based Decoding: The use of a Transformer architecture allows the decoder to attend to multiple levels of context simultaneously, facilitating a more holistic generation process compared to traditional RNN-based models.

Datasets and Evaluation

The model was trained and tested on a massive dataset derived from 10,000 top-ranked GitHub repositories. This resulted in over 18 million methods for training and 636,000 for testing. A subset of 4.2 million methods containing Javadoc documentation was specifically used to evaluate the impact of documentation context on naming accuracy.

Experimental Results

GTNM was benchmarked against four leading models: code2vec, code2seq, MNire, and DeepName. The results demonstrate a substantial performance leap across all metrics.

Figure 6: Performance comparison of GTNM against state-of-the-art baselines.

As shown in the results, GTNM achieved an Exact Match (EM) accuracy of 62.01%, representing a nearly 40% improvement over the previous best of 44.30% (DeepName). The F1 score also saw a significant rise to 75.60%. These results confirm that global context is not just helpful but essential for high-fidelity method name recommendation.

Discussion and Key Insights

The success of GTNM highlights several critical aspects of code semantics:

  • Documentation as a Primary Source: Including even the first sentence of a Javadoc comment significantly boosted the F1 score, proving that developer comments are a rich source of naming clues.
  • Consistency over Neologisms: By observing project-wide conventions, GTNM maintains stylistic consistency (e.g., choosing Encountered over Occurred if that is the project's norm).
  • Precision vs. Human Baseline: Qualitative reviews suggest that GTNM often provides more descriptive names than human developers, such as suggesting addPathIfNotExists instead of a generic add.

Conclusion

GTNM demonstrates that "global" knowledge can effectively compensate for local ambiguity in code. While currently focused on Java, the underlying principles of multi-level context encoding are language-agnostic. Future research will explore more complex program representations, such as Graph Neural Networks (GNNs), to further capture the intricate data flows within large-scale software projects.

Summary updated

BibTeX
@inproceedings{liu2022learning,
  author = {Fang Liu and Ge Li and Zhiyi Fu and Shuai Lu and Yiyang Hao and Zhi Jin},
  title = {{Learning to Recommend Method Names with Global Context}},
  booktitle = {ICSE},
  year = {2022},
  doi = {10.1145/3510003.3510154},
}
Figure