DLCoG: A Novel Framework for Dual-Level Code Comment Generation Based on Semantic Segmentation and In-Context Learning
In large software projects with collaborative development, comprehensive code comments are crucial for code readability and maintainability. Code comments mainly include method comments and inline comments, where the former describes the functionality globally, and the latter describes the implementation details locally. Existing methods typically generate these two kinds of comments with specific locations independently, which results in weak correlations between comments and code context, as well as high model inference costs due to long token inputs. To address these issues, we define the combination of inline comments and method comments as Dual-Level Code Comments. We formulate the novel task of automatically generate dual-level code comments based on given code and propose an approach named DLCoG (DualLevel Code Comment Generation) to automate this task. First, a Semantic Segmentation and Identification multi-task model based on CodeBERT, termed Se<sup xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:xlink="http://www.w3.org/1999/xlink">2</sup>Iden (Semantic Segmentation and Identification model), is proposed to identify code segments requiring inline comments. Next, we retrieve similar samples to adopting the in-context learning paradigm, which can enhance the generation quality of large language models (LLMs) in specific domains. Finally, the LLM is guided to generate duallevel code comments using Chain-of-Thought (CoT) prompts that first produce inline comments, followed by method comments. We manually constructed a high-quality clean Java dataset consisting of <Method, Method Comment, <Snippet, Inline Comments>*> based on open-source Java projects by (i) determining comments type and (ii) manually associating inline comments with their corresponding code. Then, we trained a multi-task learning model based on CodeBERT to automatically take the two steps needed, termed ICSA (Inline Comment Classification and Scope Association), thus to expand to a dataset containing 80k dual-level code comments. Experimental results on clean and extended datasets show that DLCoG outperforms all baselines by substantial margins. The contextual information provided by DLCoG can effectively improve the inline comments generated by LLM. Coordinated generation of dual-level comment also brings effective improvements to method comments, which is particularly significant when there are few contextual examples. Our work fills the long-standing gap in the dual-level code comment generation field, and can provide insights for future research in this direction. We provide open-source datasets and source code for future research.
Authors
- Zhao Wei (ORCID: https://orcid.org/0000-0002-8173-9733)
- Yingjie Xia (ORCID: https://orcid.org/0000-0002-4642-2503)
- Weihuan Min (ORCID: https://orcid.org/0009-0004-4668-0192)
- Qingyang Yan (ORCID: https://orcid.org/0000-0002-1317-8129)
- Li Kuang (ORCID: https://orcid.org/0000-0003-4975-034X)
- Zhiyang Zhang (ORCID: https://orcid.org/0000-0002-6766-8449)
- Hao Yan (ORCID: https://orcid.org/0009-0007-7631-9375)
- Haiyang Yang
Institutions
- Central South University (CN)
- Tencent (China) (CN)
- Hangzhou Dianzi University (CN)
Publication Details
- Published
- 2025-04-27
- DOI
- https://doi.org/10.1109/icpc66645.2025.00036
- Primary Topic
- Topic Modeling
- Type
- article
- Field-Weighted Citation Impact
- 0.00