NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Towards Context-Aware Code Comment Generation

Xiaohan Yu, Quzhe Huang, Zheng Wang, Yansong Feng, Dongyan Zhao

Introduction

Automatic code comment generation is a critical task for software maintenance, aiding developers in navigating and understanding complex legacy codebases. Traditional deep learning approaches typically treat a function as an isolated unit—either a sequence of tokens or an Abstract Syntax Tree (AST). However, this "tunnel vision" often fails to capture the developer's high-level intent. In Object-Oriented Programming (OOP), the semantic meaning of a function is frequently intertwined with other methods within the same class. For instance, a generic method name like append() gains specific meaning (e.g., "Adds a component to the panel") only when viewed in the context of its class environment.

Methodology: A Dual-Encoding Framework

To address the limitations of local-only models, researchers from Peking University and the University of Leeds proposed a context-aware framework that utilizes a dual-encoding strategy to capture both local logic and global class-level context.

The proposed context-aware framework architecture featuring local and global encoders.

1. Local Encoder

The local encoder employs a bidirectional Gated Recurrent Unit (bi-GRU) to process the token sequence of the target function. This captures the immediate syntax and internal logic of the code block.

2. Global Encoder (C-Graph)

The core innovation is the "C-Graph," which represents the entire class as a graph where each node corresponds to a function. A Graph Attention Network (GAT) is then used to propagate information between these nodes. This mechanism allows the model to dynamically learn which neighboring functions provide the most relevant context for the target function, effectively capturing "strategic intentions" that are not explicitly stated within the target function itself.

3. Hybrid Decoder

The generation process is handled by a decoder that integrates three components:

  • Local Attention: Focuses on tokens within the target function.
  • Global Attention: Attends to relevant neighboring functions identified by the global encoder.
  • Pointer Mechanism: Enables the model to copy specific identifiers (e.g., variable names) directly from the source code, which is vital for handling out-of-vocabulary or custom terms.

Experimental Setup

The model was evaluated on a large-scale Java dataset curated from 1,634 open-source GitHub repositories. The dataset includes over 150,000 method-comment pairs across approximately 27,000 classes. On average, each class contained 37 methods, providing a robust environment for testing contextual dependencies.

Results and Discussion

The proposed model significantly outperformed existing baselines across quantitative and qualitative metrics.

Quantitative Performance

The context-aware model achieved a BLEU score of 23.78, a substantial improvement over the standard Seq2Seq model (18.74) and the previous state-of-the-art AST-based model, Code2Seq (22.07). An ablation study revealed that removing the global encoder led to a drop of over 4 points in the BLEU score, confirming that class-level context is the primary driver of performance gains.

Qualitative Analysis

Case studies demonstrate the model's ability to resolve semantic ambiguities using class context. For example, in a scenario where a function returns a variable y2, local-only models failed to provide a descriptive comment. By looking at a related setBox method in the same class, the proposed model correctly identified y2 as a "coordinate of a point."

Qualitative comparison of generated comments showing the impact of class-level context on specific examples.

The researchers also found that while explicit function calls provide the strongest contextual signals, even "random" methods from the same class offer better context than none at all, suggesting a high degree of thematic consistency within OOP classes.

Conclusion and Future Work

This work demonstrates that "looking around" at the broader class context is essential for generating accurate and meaningful code comments. While the current implementation focuses on Java and uses a relatively simple C-Graph, it sets a new benchmark for context-aware code summarization. Future research may explore more complex graph structures, such as data-flow dependencies and inheritance relationships, and extend the framework to other object-oriented languages.

Summary updated

BibTeX
@inproceedings{yu2020towards,
  author = {Xiaohan Yu and Quzhe Huang and Zheng Wang and Yansong Feng and Dongyan Zhao},
  title = {{Towards Context-Aware Code Comment Generation}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2020},
  doi = {10.18653/v1/2020.findings-emnlp.350},
}
Figure