Abstract
Automatic code summarization is a critical task for software maintenance, yet existing models often suffer from "myopia" by focusing solely on local method bodies. We present CoCoSum, a novel framework that incorporates global context—specifically class names and inter-class relationships from UML diagrams—using a specialized Multi-Relational Graph Neural Network (MRGNN). By fusing local lexical patterns with broader architectural semantics, CoCoSum generates descriptions that are significantly more accurate and context-aware than previous state-of-the-art models.
Introduction
Writing and maintaining code comments is a tedious task that developers often neglect, leading to "code rot" where documentation is missing or outdated. While deep learning models have made strides in automatic code summarization, they typically only analyze the code within the boundary of the specific method being summarized (local context).
In Object-Oriented Programming (OOP), a method’s purpose is often tied to its enclosing class or its interactions with other classes. For instance, a method named isOccupied() inside a class called Resource should ideally mention "resource" in its summary, even if that word never appears in the method's body. Current models miss these vital semantic cues because they don't "see" the broader architecture of the software.
Methodology
CoCoSum (Contextual Code SUmmarization) uses a multi-modal Seq2Seq architecture that fuses local and global information. The framework consists of several key components:
- Local Encoders: Standard GRU-based encoders process the code tokens and the Abstract Syntax Tree (AST) to capture lexical and structural patterns within the method.
- Intra-class Context: The model uses a Transformer-based sentence embedding model to encode the name of the class containing the method.
- Inter-class Context: The authors extract Unified Modeling Language (UML) class diagrams to map relationships between classes.
Multi-Relational Graph Neural Network (MRGNN)
The core innovation of CoCoSum is the MRGNN used to encode the UML graph. The MRGNN handles four specific relationship types: Generalization, Realization, Dependency, and Association. It employs a two-level attention mechanism:
- Inner-attention: Weighs the importance of different neighbor classes within the same relationship type.
- Outer-attention: Weighs the influence of different types of relationships (e.g., inheritance vs. association).
Experimental Setup
The researchers evaluated CoCoSum on two Java-based datasets:
- CodeSearchNet: A popular public benchmark, filtered to 267,047 method-summary pairs with full repository data for UML extraction.
- CoCoNet: A new, large-scale dataset collected from GitHub (2015–2020), containing over 1.2 million methods, providing a robust benchmark for modern Java development.
Key Results
CoCoSum outperformed all baselines across four major metrics (BLEU, METEOR, ROUGE-L, and CIDER):
- Quantitative Performance: On the CodeSearchNet dataset, CoCoSum achieved a BLEU-4 score of 19.04, an 8.06% improvement over the previous best baseline. Its CIDER score saw a massive 27.78% jump.
- Stability: As code snippets get longer and more complex, CoCoSum remained significantly more stable than standard models, suggesting that global context helps "ground" the model when local code becomes noisy.
- Human Evaluation: In a blind study, professional developers rated CoCoSum's summaries higher than baselines in terms of Informativeness, Naturalness, and Similarity to ground truth.
Discussion and Key Insights
The paper demonstrates that class-level information acts as a semantic anchor. By knowing the "who" (the class) and the "who else" (related classes), the model can infer the "what" (the method's purpose) much more effectively. Furthermore, the use of a Multi-Relational GNN is crucial, as a method's logic is often more influenced by a class it inherits from than one it simply associates with.
One of the most impressive findings is that global context is "orthogonal" to local context. Adding global context modules to older models (like Code-NN and Ast-attendgru) resulted in immediate performance boosts, proving that global context is a universal missing piece in the field.
Limitations and Future Work
While successful, the current study focuses exclusively on Java. While the logic of UML applies to most OOP languages, implementation details for extraction would need adaptation. Additionally, the model relies on the first sentence of Javadocs as ground truth, which can vary in quality. Future work may explore even broader contexts, such as call graphs and data flow graphs, to further refine software understanding.
Summary updated