Automatic Generation of Text Descriptive Comments for Code Blocks

Yuding Liang, Kenny Qili Zhu

Abstract

Understanding undocumented source code is a significant bottleneck in software development. This paper presents a neural framework designed to automatically generate natural language descriptions for code blocks. By leveraging the structural information within Abstract Syntax Trees (ASTs), the proposed Code-RNN encoder and Code-GRU decoder outperform traditional sequence-to-sequence models. Experimental results on GitHub Java repositories demonstrate improved ROUGE-2 scores and more coherent, human-like summaries compared to baseline methods.

Introduction

Software maintenance often requires developers to navigate vast amounts of code, much of which lacks adequate documentation. A study of ten major Java repositories on GitHub revealed that only 15.4% of methods are commented. Existing automated documentation tools often rely on rigid templates, resulting in "robotic" comments, or topic models that treat code as a "bag of words," ignoring the critical structural semantics inherent in programming languages.

To bridge this gap, we propose a system that captures both lexical and structural semantics to produce meaningful, human-readable summaries.

Methodology

The framework utilizes two specialized neural architectures to translate code into natural language.

Code-RNN: Structural Encoding

Unlike standard models that treat code as a flat sequence of text, Code-RNN processes the program's Abstract Syntax Tree (AST). It operates in a bottom-up fashion, starting from leaf nodes—such as variable names and literals—and recursively combining them into a dense vector representation of the entire code block.

Mapping source code to its structural AST representation for neural encoding.

A key component of this stage is identifier preprocessing. The system splits "CamelCase" identifiers and resolves abbreviations (e.g., expanding rnd to random) based on local context, ensuring the model captures the developer's original intent.

Code-GRU: Context-Aware Decoding

The Code-GRU decoder transforms the encoded code vector into a descriptive sentence. The primary innovation is the "Choose Gate," a modification to the standard Gated Recurrent Unit (GRU).

The Code-RNN and Code-GRU framework generating a descriptive comment.

The Choose Gate dynamically determines which dimensions of the code block vector are most relevant at each generation step. This allows the model to focus on specific logical components—such as return values or initialization logic—as it constructs the final summary.

Experimental Evaluation

The framework was evaluated using two datasets:

  1. Google Code Jam (2008–2016): 10,724 methods used to test the model's ability to classify code functionality.
  2. GitHub Java Repositories: Ten active projects (e.g., neo4j, guava) used for the comment generation task, focusing on methods with descriptive comments longer than eight words.

Key Results

  • Structural Superiority: Code-RNN outperformed baseline language embeddings in functionality classification, confirming that AST-based structural information is a superior predictor of code purpose than keywords alone.
  • Generation Accuracy: Code-GRU consistently surpassed standard seq2seq models. In the jersey repository, it achieved a ROUGE-2 score of 0.230, compared to 0.183 for the baseline.
  • Qualitative Quality: Generated comments were found to be more coherent and less repetitive than those from template-based or basic RNN models, accurately reflecting complex logic like conditional states.

Conclusion and Future Work

This research demonstrates that treating code as a structured tree rather than a flat sequence significantly improves automated comment generation. Future work includes incorporating call graphs to provide inter-method context and restoring punctuation to improve the readability of generated text. While currently tested on Java, the framework's reliance on formal parsers makes it potentially applicable across various programming languages.

Summary updated

BibTeX
@inproceedings{liang2018automatic,
  author = {Yuding Liang and Kenny Qili Zhu},
  title = {{Automatic Generation of Text Descriptive Comments for Code Blocks}},
  booktitle = {AAAI Conference on Artificial Intelligence},
  year = {2018},
  doi = {10.1609/aaai.v32i1.11963},
}
Figure