Abstract
Appropriate comments of code snippets provide insight for code functionality, which are helpful for program comprehension. However, due to the great cost of authoring with the comments, many code projects do not contain adequate comments. Automatic comment generation techniques have been proposed to generate comments from pieces of code in order to alleviate the human efforts in annotating the code. Most existing approaches attempt to exploit certain correlations (usually manually given) between code and generated comments, which could be easily violated if the coding patterns change and hence the performance of comment generation declines. In this paper, we first build C2CGit, a large dataset from open projects in GitHub, which is more than 20$\times$ larger than existing datasets. Then we propose a new attention module called Code Attention to translate code to comments, which is able to utilize the domain features of code snippets, such as symbols and identifiers. We make ablation studies to determine effects of different parts in Code Attention. Experimental results demonstrate that the proposed module has better performance over existing approaches in both BLEU and METEOR.
Introduction
Writing high-quality code comments is a cornerstone of sustainable software maintenance, yet it remains a time-consuming task often sidelined in rapid development cycles. While Neural Machine Translation (NMT) has emerged as a potential solution for "translating" source code into natural language descriptions, traditional models often fail by treating code as simple text. Source code is inherently structured; for instance, nested control flow structures like for loops carry specific functional meanings that generic text processors overlook. Furthermore, the field has been hampered by small, curated datasets that do not reflect the complexity of real-world software.
To address these challenges, we introduce Code Attention, a novel neural architecture that explicitly models the structural and domain-specific features of code. We also present C2CGit, a massive new dataset crawled from GitHub that provides a more robust benchmark for the community.
Methodology
The core of our approach is a 3-layer Gated Recurrent Unit (GRU) encoder-decoder model enhanced by a specialized Code Attention mechanism. Unlike standard attention modules, Code Attention incorporates three domain-aware components:
- Identifier Ordering: The system renames identifiers based on their nesting depth (e.g.,
FOR1,FOR2) to help the model perceive the code's hierarchy. - Token Encoding: A specialized dictionary distinguishes between language keywords, mathematical symbols, and user-defined variables.
- Global Attention: This module uses learned weights to emphasize critical tokens, such as symbols and identifiers, during the translation process.
Our research indicates that structural context is often more important than lexical information alone. For example, a "shift" operation might look like a "swap" lexically, but its structural context—which Code Attention captures—reveals its true intent. Interestingly, we found that RNN-based structures outperformed pure Transformers for this task, likely due to their ability to capture the sequential, long-term dependencies inherent in programming logic.
The C2CGit Dataset
We introduce C2CGit, a large-scale benchmark for code-to-comment translation. The dataset was constructed by crawling over 1,600 open-source Java projects on GitHub, resulting in 879,994 code-comment pairs.
As shown in Figure 4, C2CGit is approximately 20 times larger than previous standards like CODE-NN. It features longer code snippets and a wider diversity of commenting styles, providing a more realistic representation of modern software development.
Experimental Results
We evaluated Code Attention against several baselines using both automated metrics and human assessments.
- Automated Metrics: Our model achieved a BLEU-4 score of 24.62, significantly outperforming standard GRU-NN (16.72) and the MOSES translation system (9.54).
- Human Evaluation: Professional Java developers rated the model's output at 4.08 in Understandability on a 5-point scale, surpassing the 3.70 score of the next best model (LSTM-NN).
- Ablation Study: We confirmed that our "Global Attention" mechanism, utilizing dot-product amplification, increased the BLEU-4 score by over 2 points compared to standard additive attention.
Conclusion and Future Work
Code Attention demonstrates that incorporating domain-specific structural hints—such as identifier nesting and symbol weighting—significantly improves the quality of automatically generated comments. While the current model relies on sequential representations, code is fundamentally a tree structure. Our future work will focus on integrating Abstract Syntax Trees (AST) directly into the Code Attention module to further deepen the model's understanding of complex programming logic.
Summary updated