Introduction
Code summarization—the task of automatically generating a natural language description for a snippet of code—is vital for software maintenance. To do this well, models need to understand not just the "text" of the code (tokens), but its underlying logic and structure. Researchers usually represent this structure using Abstract Syntax Trees (ASTs). However, existing AST-based models face a "Goldilocks" problem: tree-based models are powerful but extremely slow and unstable when trees are deep, while linearization methods flatten the tree into a sequence, losing the hierarchical "shape" of the code.
Methodology: The CAST Framework
The authors introduce CAST (Code Summarization with hierarchical splitting and reconstruction of ASTs), a novel model that improves automated code documentation by breaking down large, complex ASTs into manageable subtrees. The framework follows a two-phase "divide and conquer" approach:
Hierarchical Splitting
Instead of treating the AST as one giant tree, CAST uses a set of rules to split it into a "Structure Tree" of smaller subtrees. It identifies logical blocks like method signatures, initialization statements, and nested structures (e.g., if or for loops). Crucially, when a nested block is split out, a "placeholder" is left behind, preserving the knowledge of where that block belongs.
Two-Phase Encoding and Hybrid Decoding
- Phase 1: A Recursive Neural Network (RvNN) encodes each individual subtree into a vector.
- Phase 2: A second RvNN aggregates these subtree vectors based on their positions in the "Structure Tree," reconstructing the global hierarchy.
Finally, the model uses a Transformer-based decoder that looks at both the reconstructed AST representation and the raw code tokens. It also employs a Copy Mechanism, allowing the model to "copy" specific variable or function names directly from the source code into the summary.
Experimental Results
The model was evaluated on two large-scale Java datasets: TL-CodeSum (83,661 pairs) and Funcom (over 2.1 million pairs). CAST significantly outperformed state-of-the-art baselines like Code2seq, ASTNN, and NCS across multiple metrics.
Accuracy and Efficiency
On the Funcom dataset, CAST achieved a BLEU score of 30.83, compared to 28.27 for the next best AST-based model (CodeAstnn). Beyond accuracy, CAST offers massive efficiency gains. While some tree-based models took 21 hours per training epoch, CAST took only 10 to 40 minutes per epoch, making it much more practical for real-world use.
Human Evaluation
In a study with 10 developers, CAST was rated highest for Informativeness (how much content it captured), Naturalness (fluency), and Similarity to ground truth.
Conclusion
CAST demonstrates that splitting ASTs at the block level (loops, logic branches) is superior to splitting at the statement level. Block-level subtrees contain relatively complete semantics, making it easier for neural networks to learn code functionality. By breaking deep trees into shallow subtrees, CAST avoids "gradient vanishing" problems while preserving global context through hierarchical reconstruction.
Summary updated