An Intra-Class Relation Guided Approach for Code Comment Generation (2023)

Abstract: Code comments are critical for maintaining and comprehending software programs, but they are often missing, mismatched, or outdated in practice. Code comment generation task aims to automatically produce descriptive comments for code snippets. Recently, methods based on the neural encoder-decoder architecture have achieved impressive performance. These methods assume that all the information required to generate comments is encoded in the target function itself, yet in most realistic situations, it is hard to understand a function in isolation from the surrounding context. Furthermore, the global context may contain redundant information that should not be introduced. To address the above issues, we present a novel graph-based learning framework to capture various relations among functions in a class file. Our approach is based on a common real-world scenario in which only a few functions in the source file have human-written comments. Guided by intra-class function relations, our model incorporates contextual information extracted from both the source code and available comments to generate missing comments. We conduct experiments on a Java dataset collected from real-world projects. Experimental results show that the proposed method outperforms competitive baseline models on all automatic and human evaluation metrics.

## introduction code comment generation is the task of automatically producing natural language descriptions for given code snippets. appropriate and sufficient comments are essential for software maintenance and understanding @xcite . they allow developers to grasp the purpose of source code quickly and accurately. however, in real-life software projects, comments are often missing, incomplete or outdated @xcite . existing comments will also need to be adjusted as the associated programs are updated, which could cause large time and labor costs. hence, there is a significant need for automatic generation technologies that can effectively produce high-quality comments. recent works in code comment generation take the neural encoder-decoder architecture as their cornerstone @xcite @xcite @xcite . however, these works only utilize the information provided by the target function itself. in object-oriented programming, classes are the building blocks that express algorithmic intentions and they encapsulate the interaction between functions. therefore, the class-level contextual information should not be ignored when we attempt to generate code comments. there are some existing studies that attempt to fill this gap. @xcite encode all functions in a source file using gru @xcite and apply an attention mechanism to learn associations between the encoding results to words in the generated comment. @xcite construct a class graph that connects the target function to all other functions in the same class to aggregate contextual information. @xcite present a project-level encoder to augment existing models by introducing contextual information. although the above methods have shown promising performance, the way they introduce contextual information is somewhat crude. since not all surrounding functions are closely related to the target function, indiscriminately utilizing the whole context may introduce noise, which would hurt the model performance. we propose that considering function relations is a better way to leverage the contextual information. for example, in this paper, we propose a graph-based encoderdecoder learning framework for code comment generation. our approach is based on a common scenario where only a few functions in the class file are documented. we construct a heterogeneous graph to model both the extractive relation and inductive relation among functions within a class file. in the encoding stage, we encode all functions and available comments using bi-gru. then, we design an intra-class relational gat encoder to aggregate information and perform a fusion of both types of relations via a cross-graph mechanism. in the decoding stage, we employs a gru decoder with a by-pointer mechanism to generate a comment utilizing the encoding results. to evaluate the performance of our approach, we gather a java dataset that preserves the class structure. we conduct experiments on this dataset and perform evaluation using automatic and human evaluation metrics. the experimental results show that our model outperforms prior methods by a significant margin, which demonstrate the effectiveness of our proposed framework. ## related works early efforts on code comment generation are template-based or information retrieval (ir) based approaches @xcite @xcite @xcite . in recent years, the neural encoder-decoder architecture is employed to the code comment generation field, which was designed for neural machine translation (nmt) task originally. codenn @xcite ) is an early work that attempts to adopt the encoderdecoder architecture for generating code comments. followed works develop a variety of models by introducing the abstract syntax tree (ast) to extract structural information of the source code @xcite @xcite @xcite . more recently, novel code representations are learned via well-designed encoders, such as gnn-based encoders @xcite and pre-training encoders @xcite @xcite . hybrid methods that integrate the ir-based and neural-based techniques proved to perform well on the code comment generation task. @xcite retrieve two similar code snippets of the target function at syntax and semantics levels, then utilize their encoding information to generate comments in the decoding stage. @xcite retrieve the most similar code snippet and the corresponding comment to assist the generation process. @xcite retrieve the most similar codecomment pair and add it as auxiliary information to their proposed hybrid gnn framework. however, these works rarely utilize contextual information that is external to the target function. (section 3.4). lastly, these encoding results are fed into the decoder to create the target comment (section 3.5). some of the most recent works make efforts to bridge this gap @xcite @xcite . in contrast to these existing approaches, our method explores function relationships within a class and only incorporates related functions. this has the advantage of avoiding noise caused by irrelevant functions and focusing on the valuable contextual information. ## approach this section introduces our proposed framework for code comment generation. figure 1 illustrates an overview of our approach. ## conclusion in this paper, we propose a graph-based learning framework for code comment generation. our approach targets a practical scenario where only a few functions in the class file have human-written comments. to identify valuable information from the class context, we model function relations and develop a graph attention network to aggregate class-level contextual information. we conducted experiments on java programs collected from realworld projects and the results demonstrate that our approach outperforms prior methods.