Abstract
Code comment generation often produces generic descriptions that fail to meet the diverse needs of developers. We present KUMIC, a framework for multi-intent comment generation that optimizes knowledge retrieval and presentation for Large Language Models (LLMs). KUMIC employs a quality-aware example retriever and a mapping knowledge chain to identify intent-specific important code statements. Experimental results on two large-scale Java datasets show that KUMIC significantly outperforms state-of-the-art baselines in accuracy, adequacy, and intent alignment.
Introduction
In modern software engineering, a single "monolithic" comment is rarely sufficient for all stakeholders. For instance, a backend developer may require the design rationale (why), while a frontend developer might only need API usage instructions (how-to-use). This diversity of information needs has given rise to the field of multi-intent code comment generation.
While Large Language Models (LLMs) have demonstrated impressive code understanding capabilities, they face two primary challenges in this specific task:
- Example Quality Negligence: Standard few-shot prompting often selects demonstration examples based solely on code similarity, ignoring whether the associated comment is a high-quality match for that code.
- Lack of Explicit Guidance: LLMs frequently struggle to distinguish which specific lines of code contribute to a particular intent, often becoming distracted by irrelevant implementation details.
The KUMIC Framework
To address these challenges, the authors propose KUMIC, which optimizes how knowledge is retrieved and presented to an LLM through three core components.
1. Example Retriever with Quality Assessment
KUMIC moves beyond simple code similarity. It evaluates the "semantic consistency" between the code and the comment in potential demonstration examples. By filtering for pairs where the comment is clearly and accurately derived from the code, KUMIC ensures the LLM learns from high-quality, noise-free demonstrations.
2. Knowledge Augmentation via Mapping Chains
To guide the LLM's focus, KUMIC identifies "intent-specific important statements." It utilizes a lightweight code search model (a cross-encoder) to analyze the attention matrix, determining which lines of code correlate most strongly with a specific intent (e.g., What vs. Property). This creates a "Mapping Knowledge Chain" (Code → Important Statements → Comment) that prevents hallucination.
3. Chain-of-Thought (CoT) Prompting
The final prompt follows a structured two-step reasoning process: first, extracting the important statements from the code, and second, generating the intent-specific comment based on those extracted statements.
Experimental Setup
The researchers evaluated KUMIC on two large-scale Java datasets: Funcom (originally 2.1M pairs) and TLC (87,000 pairs from high-star GitHub projects). Comments were categorized into five primary intents: What, Why, How-to-use, How-it-is-done, and Property.
KUMIC was tested against baselines such as DOME (a Transformer-based model) and FSMIC (a standard few-shot LLM approach) using several state-of-the-art LLM backbones, including CodeLlama, Llama3, and Qwen2.5-Coder.
Results and Analysis
Quantitative results demonstrate that KUMIC consistently outperforms existing methods across all metrics and datasets.
Key findings include:
- Significant Performance Gains: KUMIC outperformed baselines by an average of 14.49% in BLEU, 22.41% in METEOR, and 12.94% in SBERT scores.
- Sample Efficiency: KUMIC with only 3 demonstration examples often outperformed other models using 10 examples, highlighting the value of high-quality example selection.
- Human Validation: In a blind study with Java developers, KUMIC scored significantly higher in Accuracy, Adequacy, and Intention capture. The framework proved particularly effective for "semantically demanding" intents like What and Why.
Conclusion and Future Work
KUMIC demonstrates that optimizing knowledge utilization through semantic consistency and explicit intent-mapping significantly improves the quality of multi-intent code comments. While currently focused on Java, the underlying logic is language-agnostic. Future work will involve expanding the framework to support other programming languages and incorporating repository-level context to further enhance comment relevance.
Summary updated