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

CodeExp: Explanatory Code Document Generation

Haotian Cui, Chenglong Wang, Junjie Huang, Jeevana Priya Inala, Todd Mytkowicz, Bo Wang, Jianfeng Gao, Nan Duan

Abstract

Developing models that can automatically generate detailed code explanation can greatly benefit software maintenance and programming education. However, existing code-to-text generation models often produce only high-level summaries of code that do not capture implementation-level choices essential for these scenarios. To fill in this gap, we propose the code explanation generation task. We first conducted a human study to identify the criteria for high-quality explanatory docstring for code. Based on that, we collected and refined a large-scale code docstring corpus and formulated automatic evaluation metrics that best match human assessments. Finally, we present a multi-stage fine-tuning strategy and baseline models for the task. Our experiments show that (1) our refined training dataset lets models achieve better performance in the explanation generation tasks compared to larger-scale unrefined data (15x larger), and (2) fine-tuned models can generate well-structured long docstrings comparable to human-written ones.

Introduction

While AI-powered tools like GitHub Copilot have revolutionized code generation, documenting that code remains a significant challenge. Most current "code-to-text" models focus on high-level summarization—for instance, stating that a function "calculates a sum." However, for developers and students, understanding the how and why is often more critical than the what. Professional documentation requires explaining implementation choices, parameter constraints, and internal logic.

The primary obstacles in this field are the lack of high-quality, detailed training data and the misalignment of standard automatic metrics (like ROUGE) with human judgment of explanation quality. To address this, we introduce CodeExp, a framework and dataset designed to move beyond simple summaries toward comprehensive explanatory docstrings.

The CodeExp Corpus

A key contribution of this work is the creation of a high-quality corpus for code explanation. We partitioned the data into three distinct levels to facilitate both training and evaluation.

Statistics of the CodeExp dataset partitions.

The CodeExp(raw) set contains 2.3 million Python function-docstring pairs. To improve quality, we trained a BERT-based filter on a small human-annotated set to identify high-quality docstrings, resulting in the CodeExp(refined) subset. Finally, CodeExp(annotated) provides a gold-standard set for rigorous evaluation.

Methodology: Two-Stage Fine-Tuning

We propose a multi-stage fine-tuning strategy, termed S3, which follows a curriculum learning approach.

  1. General Learning: The model is first fine-tuned on the massive but noisy raw dataset to learn broad language-code associations.
  2. Refinement: The model is then fine-tuned on the smaller, high-quality refined dataset. This stage polishes the model's output, teaching it the structure and detail required for professional-grade documentation.

Evaluation and Results

Our experiments compared several backbone models, including GPT-2, GPT-Neo, and CodeT5. We evaluated these models across four human-centric aspects: behavior (A1), coherence (A2), fluency (A3), and coverage (A4).

Human evaluation results across four aspects (A1-A4) and overall score.

The results demonstrate that specialized fine-tuning is highly effective. Notably, CodeT5-(refined) and the two-stage CodeT5-(r+r) models achieved performance comparable to human-written references. In fact, the fine-tuned CodeT5 model (220M parameters) outperformed the zero-shot performance of the much larger OpenAI Codex (12B parameters) in several categories.

Key Insights

  • Quality over Quantity: Training on the 158k "refined" pairs yielded significantly better results than training on the 2.3M "raw" pairs. This highlights that noise in large datasets can actively hinder the generation of detailed explanations.
  • Metric Alignment: We found that BLEU and METEOR scores align most closely with human judgment for this task, whereas ROUGE is less reliable for evaluating code explanations.
  • The Coverage Challenge: Both models and humans still find it difficult to achieve perfect "coverage"—explaining every logical branch (e.g., try/except blocks) within a function.

Conclusion

CodeExp demonstrates that high-quality, refined data and a curriculum-based fine-tuning strategy can produce code explanations that rival human documentation. While these models are fluent and informative, future work is needed to ensure "semantic verification"—guaranteeing that the generated text is factually accurate relative to the code's logic. We believe our dataset and evaluation protocol will serve as a foundation for future research in automated code documentation.

Reference: Cui, H., Wang, C., Huang, J., et al. (2022). CodeExp: Explanatory Code Document Generation. Findings of EMNLP 2022. GitHub Repository

Summary updated

BibTeX
@inproceedings{cui2022codeexp,
  author = {Haotian Cui and Chenglong Wang and Junjie Huang and Jeevana Priya Inala and Todd Mytkowicz and Bo Wang and Jianfeng Gao and Nan Duan},
  title = {{CodeExp: Explanatory Code Document Generation}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2022},
  url = {https://openreview.net/forum?id=4AW3Vflf5Q},
}
Figure