CAST : Enhancing Code Summarization with Hierarchical Splitting and Reconstruction of Abstract Syntax Trees (2021)

Abstract: Code summarization aims to generate concise natural language descriptions of source code, which can help improve program comprehension and maintenance. Recent studies show that syntactic and structural information extracted from abstract syntax trees (ASTs) is conducive to summary generation. However, existing approaches fail to fully capture the rich information in ASTs because of the large size/depth of ASTs. In this paper, we propose a novel model CAST that hierarchically splits and reconstructs ASTs. First, we hierarchically split a large AST into a set of subtrees and utilize a recursive neural network to encode the subtrees. Then, we aggregate the embeddings of subtrees by reconstructing the split ASTs to get the representation of the complete AST. Finally, AST representation, together with source code embedding obtained by a vanilla code token encoder, is used for code summarization. Extensive experiments, including the ablation study and the human evaluation, on benchmarks have demonstrated the power of CAST. To facilitate reproducibility, our code and data are available at https://github.com/DeepSoftwareAnalytics/CAST .

## introduction code summaries are concise natural language descriptions of source code and they are important for program comprehension and software maintenance. however, it remains a labor-intensive and time-consuming task for developers to document code with good summaries manually. over the years, many code summarization methods have been proposed to automatically summarize program subroutines. traditional approaches such as rule-based and information retrievalbased approaches regard source code as plain † the first two authors contribute equally. § yanlin wang and hongbin sun are the corresponding authors. we have identified some limitations of the existing ast-based approaches, which lead to a slow training process and/or the loss of ast structural information. we now use an example shown in fig. 1 to illustrate the limitations: to overcome the above limitations, we propose a novel model cast (code summarization with hi-erarchical splitting and reconstruction of abstract syntax trees). the key idea of our approach is to split an ast (fig. 1(b) ) into a set of subtrees (fig. 1(c )) at a proper granularity and learn the representation of the complete ast by aggregating its subtrees' representation learned using tree-based neural models. first, we split a full ast in a hierarchical way using a set of carefully designed rules. second, we use a tree-based neural model rvnn to learn each subtree's representation. third, we reconstruct the split asts and combine all subtrees' representation by another rvnn to capture the full tree's structural and semantic information. finally, the representation of the complete tree, together with source code embedding obtained by a vanilla code token encoder, is fed to a transformer decoder to generate descriptive summaries. take fig. 1(a ) for example again: there are two sub-sentences in the reference summary. the for block (lines 6, 7 and 13 in fig. 1(a) ) corresponds to the first subsentence "loop through each of the columns in the given table", and the if block (line 8-12) corresponds to the second sub-sentence "migrating each as a resource or relation". the semantics of each block can be easily captured when the large and complex ast is split into five subtrees as shown in fig. 1(c ). after splitting, t 5 corresponds to first sub-sentence and t 4 corresponds to the second subsentence. when we reconstruct the split asts according to fig. 1(d ), it is easier for our approach to generate the summary with more comprehensive semantics. our method cast has two-sided advantages: (1) tree splitting reduces ast to a proper size to allow effective and affordable training of treebased neural models. (2) different from previous work, we not only split trees but also reconstruct the complete ast using split asts. this way, high-level hierarchical information of asts can be retained. we conduct experiments on tl-codesum @xcite and funcom @xcite datasets, and compare cast with the state-of-theart methods. the results show that our model outperforms the previous methods in four widely-used metrics bleu-4, rouge-l, meteor and cider, and significantly decreases the training time compared to hybriddrl. we summarize the main contributions of this paper as follows: there are also works that utilize ensemble model @xcite or pre-trained models @xcite @xcite to model source code. ## cast: code summarization with ast splitting and reconstruction this section presents the details of our model. the architecture of cast (fig. 2 ) follows the general seq2seq framework and includes three major components: an ast encoder, a code token encoder, and a summary decoder. given an input method, the ast encoder captures the semantic and structural information of its ast. the code token encoder encodes the lexical information of the method. the decoder integrates the multi-channel representations from the two encoders and incorporates a copy mechanism @xcite to generate the code summary. ## threats to validity there are three main threats to validity. first, we evaluate and compare our work only on a java dataset, although in principle, the model should generalize to other languages, experiments are needed to validate it. also, ast splitting algorithm need to be implemented for other languages by implementing a visitor to ast. second, in neural network model design, there are many orthogonal aspects such as different token embeddings, whether to use beam search, teacher forcing. when showing the generality of cast, we have done the experiments in a controlled way. a future work might be to do all experiments in a more controlled way and the performance of cast could rise further when combined with all other orthogonal techniques. third, summaries in the datasets are collected by extracting the first sentences of javadoc. although this is a common practice to place a method's summary at the first sentence of javadoc, there might still be some mismatch summaries. a higher quality dataset with better summaries collecting techniques is needed in the future. ## conclusion in this paper, we propose a new model cast that splits the ast of source code into several subtrees, embeds each subtree, and aggregates subtrees' information back to form the full ast representation. this representation, along with code token sequence information, is then fed into a decoder to generate code summaries. experimental results have demonstrated the effectiveness of cast and confirmed the usefulness of the abstraction technique. we believe our work sheds some light on future research by pointing out that there are better ways to represent source code for intelligent code understanding.