Skip to main content
QUICK REVIEW

[Paper Review] Code Attention: Translating Code to Comments by Exploiting Domain Features

Wenhao Zheng, Hong-Yu Zhou|arXiv (Cornell University)|Sep 22, 2017
Software Engineering ResearchComputer Science31 references12 citations
TL;DR

This paper proposes Code Attention, a novel attention mechanism that improves code-to-comment generation by explicitly modeling domain-specific features such as identifiers, symbols, and program structure. By integrating these features into a sequence-to-sequence RNN framework, the method achieves state-of-the-art performance on BLEU, METEOR, and human evaluation, outperforming existing approaches on a new large-scale dataset, C2CGit.

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$ imes$ 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.

Motivation & Objective

  • To address the lack of adequate comments in open-source code, which hinders program comprehension and maintenance.
  • To overcome the limitations of existing comment generation methods that rely on rigid, manually defined code-comment correlations that break under changing coding patterns.
  • To develop a more robust and generalizable approach to code-to-comment translation by exploiting intrinsic domain features of code.
  • To build a large-scale, real-world benchmark dataset to support and validate future research in code comment generation.
  • To demonstrate that explicitly modeling code structure and semantic features leads to more accurate and functionally correct comments.

Proposed method

  • Proposes a new attention mechanism called Code Attention that integrates domain-specific features—such as identifiers, symbols, and control structures—into the attention computation.
  • Designs a three-stage process: Identifier Ordering to prioritize meaningful tokens, Token Encoding to embed code elements, and Global Attention to align code features with natural language output.
  • Employs a 3-layer RNN encoder-decoder architecture with beam search inference, trained on the C2CGit dataset using stochastic gradient descent.
  • Uses bucketing to handle variable-length code and comment pairs, with optimal bucket sizes (40,15), (55,20), (70,40), (220,60) selected via 10-fold cross-validation.
  • Applies adaptive learning rate decay (initial 0.5, decayed by 0.99 every 3k non-decreasing steps) to improve training stability and convergence.
  • Leverages pre-trained word embeddings (512-dim) and hidden units of size 1024 to capture semantic and syntactic patterns in code and comments.

Experimental results

Research questions

  • RQ1Can a neural attention mechanism that explicitly models code-specific features like identifiers and symbols improve code-to-comment generation?
  • RQ2Does incorporating program structure (e.g., loops, conditionals) into the attention mechanism lead to more functionally accurate comments?
  • RQ3How does the performance of the proposed method compare to existing baselines on both automatic metrics and human evaluation?
  • RQ4To what extent does a larger, real-world dataset like C2CGit improve the generalization and reliability of code comment generation models?
  • RQ5What is the contribution of each component in the Code Attention module to the overall performance?

Key findings

  • The proposed Code Attention model achieves state-of-the-art performance on both automatic metrics, outperforming all baselines on BLEU and METEOR scores.
  • Human evaluation confirms that comments generated by Code Attention are more accurate and semantically aligned with code functionality than those from existing methods.
  • Ablation studies show that each component of Code Attention—identifier ordering, token encoding, and global attention—contributes significantly to performance gains.
  • The C2CGit dataset, containing over 1,000 open-source projects, is more than 20 times larger than previous benchmarks, enabling more robust model evaluation.
  • The model generates comments that correctly reflect structural semantics (e.g., distinguishing 'swap' from 'shift') that simpler models miss.
  • Training the model on a single NVIDIA K80 GPU took approximately 3 days and 90,000 iterations, indicating feasible training efficiency for the architecture.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.