arXiv:2503.10737cs.SEcs.AI2025-03被引 14

用大模型为文件和模块级代码生成注释,提升开发效率。

Commenting Higher-level Code Unit: Full Code, Reduced Code, or Hierarchical Code Summarization

  • 分三种策略:全量代码、精简代码、分层代码摘要
  • 文件级用全量代码效果最好,模块级用分层策略最优
  • 大模型可自动评估注释质量,结果接近人工评分

代码注释是软件开发中的关键环节,有助于后续维护与更新。为提高注释效率、减轻开发者负担,研究者提出了多种自动化代码摘要(ACS)技术,用于自动生成代码单元的注释。然而,现有方法主要集中在方法级代码摘要,对文件级和模块级等高层代码单元的研究严重不足。尽管这些高层代码的摘要对快速理解软件架构和组件至关重要。为此,本文系统研究如何利用大语言模型(LLM)对文件级和模块级代码进行注释。由于高层代码远大于方法级,面临长文本输入限制和效率挑战。我们探索了三类摘要策略:全量代码摘要、精简代码摘要和分层代码摘要。实验表明,对于文件级代码,全量代码摘要最有效,精简代码为成本效益更高选择;而对于模块级代码,分层摘要策略表现最佳。此外,受方法级研究启发,我们还考察了将大模型作为摘要质量评估器,实验显示其评估结果与人工评价高度相关。

原文摘要 · Abstract (English)

Commenting code is a crucial activity in software development, as it aids in facilitating future maintenance and updates. To enhance the efficiency of writing comments and reduce developers' workload, researchers has proposed various automated code summarization (ACS) techniques to automatically generate comments/summaries for given code units. However, these ACS techniques primarily focus on generating summaries for code units at the method level. There is a significant lack of research on summarizing higher-level code units, such as file-level and module-level code units, despite the fact that summaries of these higher-level code units are highly useful for quickly gaining a macro-level understanding of software components and architecture. To fill this gap, in this paper, we conduct a systematic study on how to use LLMs for commenting higher-level code units, including file level and module level. These higher-level units are significantly larger than method-level ones, which poses challenges in handling long code inputs within LLM constraints and maintaining efficiency. To address these issues, we explore various summarization strategies for ACS of higher-level code units, which can be divided into three types: full code summarization, reduced code summarization, and hierarchical code summarization. The experimental results suggest that for summarizing file-level code units, using the full code is the most effective approach, with reduced code serving as a cost-efficient alternative. However, for summarizing module-level code units, hierarchical code summarization becomes the most promising strategy. In addition, inspired by the research on method-level ACS, we also investigate using the LLM as an evaluator to evaluate the quality of summaries of higher-level code units. The experimental results demonstrate that the LLM's evaluation results strongly correlate with human evaluations.

代码摘要大模型软件工程分层摘要

Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。