Introduction
Automatic code summarization—the task of generating a concise natural language description of a code snippet—is a critical component for modern software maintenance and documentation. Despite recent advances in Large Language Models (LLMs), two primary challenges persist in this field. First, the heterogeneity gap between structured, logic-heavy source code and fluid, semantic natural language makes direct mapping difficult. Second, most existing models lack a verification mechanism; unlike human developers who review and refine their comments, standard models typically operate as one-shot generators without a "rethinking" phase.
To address these issues, this paper introduces RBCoSum, a framework that mimics the human "write-then-review" process. By utilizing a "Chain of Comments" (COC) to bridge the structural gap and a learned ranker to select the optimal summary, RBCoSum achieves state-of-the-art performance in code summarization tasks.
Methodology
The RBCoSum framework operates in two distinct phases: Summarization with Chain of Comments (SCOC) and Rethinking (Reranking).
Phase 1: Summarization with Chain of Comments (SCOC)
To bridge the heterogeneity gap, the framework first generates a Chain of Comments (COC) using an LLM (e.g., GPT-3.5-Turbo). The COC provides a step-by-step breakdown of the code's logic, serving as an intermediate "knowledge bridge." A summarizer (such as CodeT5 or StarCoder) is then fine-tuned using the original code concatenated with this COC. This approach allows the model to follow a logical "plan" rather than attempting to synthesize complex functionality in a single step.
Phase 2: Rethinking (Reranking)
In the second phase, the framework samples multiple candidate summaries from the fine-tuned model. A learned ranker, built on CodeBERT with a classification head, scores these candidates based on their alignment with the source code. This rethinking step ensures that the final output is not just the most probable sequence from the generator, but the one that best captures the semantic essence of the code.
Experimental Setup
The researchers evaluated RBCoSum on the XLCoST benchmark, focusing on Java and Python. To train the ranker, they constructed a massive ranking dataset containing over 1.39 million instances. This dataset was built by generating candidates from four different LLMs (StarCoder, Code Llama, CodeT5+, and Incoder) across multiple source datasets. Summaries were labeled as positive or negative based on a composite score of traditional NLP metrics (BLEU, ROUGE, METEOR) and semantic similarity (SIM).
Key Results and Insights
RBCoSum significantly outperformed state-of-the-art baselines across multiple metrics. On the XLCoST-Java dataset, RBCoSum (using CodeT5) achieved an average score of 41.22, a notable improvement over the base CodeT5 model's 39.15.
Sensitivity and Ablation Analysis
As shown in Figure 6, the framework's performance is influenced by several factors:
- Sample Size: Increasing the number of candidate samples generally improves performance, particularly when looking at the "ceiling" (the best possible summary available in the sample set).
- Ranking Data Size: The quality of the ranker scales with the amount of training data, with larger base models (e.g., CodeT5-large) showing the most significant gains as data size increases.
- The "Bridge" Effect: Ablation studies revealed that the content of the COC is more important than its order. Shuffling COC sentences had a minimal impact, whereas replacing them with random text caused performance to plummet, proving the model extracts specific semantic facts from the comments.
- Human Evaluation: In blind studies, human evaluators consistently rated RBCoSum summaries as more natural, informative, and truthful compared to base models.
Conclusion and Future Work
RBCoSum demonstrates that bridging the gap between code and natural language through intermediate logic (COC) and adding a verification step (Rethinking) can substantially improve automatic summarization. While the current implementation is computationally more intensive than one-shot models, it provides a robust path toward more accurate and human-like documentation. Future work will focus on optimizing the framework for real-time use and expanding its validation to a broader range of programming languages.
Summary updated