Abstract
Descriptive code comments are essential for supporting code comprehension and maintenance. We propose the task of automatically generating comments for overriding methods. We formulate a novel framework which accommodates the unique contextual and linguistic reasoning that is required for performing this task. Our approach features: (1) incorporating context from the class hierarchy; (2) conditioning on learned, latent representations of specificity to generate comments that capture the more specialized behavior of the overriding method; and (3) unlikelihood training to discourage predictions which do not conform to invariant characteristics of the comment corresponding to the overridden method. Our experiments show that the proposed approach is able to generate comments for overriding methods of higher quality compared to prevailing comment generation techniques.
Introduction
In object-oriented programming, "overriding" a method is a fundamental practice where a subclass provides a specific implementation of a method already defined by its parent (superclass). While comments are essential for code maintenance, writing them is tedious and often neglected. Existing automatic comment generation tools typically look only at the local method body. However, overriding methods are unique: their purpose is often a mix of the parent’s general functionality and the child’s specific tweaks.
For example, if a parent method getEncoded() returns a generic byte array, a subclass might override it to return a specific "ASN.1 encoded" version. A standard model might generate a generic comment like "returns the encoding," missing the crucial "ASN.1" detail that makes the override necessary. To address this, researchers at UT Austin developed HIERARCHY-AWARE SEQ2SEQ, a framework that leverages class hierarchies—specifically the parent class's comments and the subclass's name—to produce documentation that is both more accurate and more specific.
Methodology
The HIERARCHY-AWARE SEQ2SEQ architecture moves beyond looking at a single snippet of code by incorporating three distinct input streams:
- Triple-Encoder Architecture: The model uses separate encoders to process the overriding method body ($M^\perp$), the subclass name ($Kname^\perp$), and the superclass comment ($C^\top$). The superclass comment serves as a structural template, while the class name often contains clues about the customization (e.g.,
InfoAccessSyntax). - Conditioning on Specificity: Standard models often generate generic, high-frequency words. To combat this, the authors use Normalized Inverse Word Frequency (NIWF) to measure comment specificity. The model is trained to condition on a "specificity level," allowing it to prefer more descriptive technical terms during inference.
- Unlikelihood Training: To ensure the new comment retains the core logic of the parent, an "unlikelihood" objective penalizes the model if it fails to retain invariant keywords from the superclass comment that should still apply to the subclass.
Experimental Setup
The researchers constructed a large-scale parallel corpus from open-source Java projects on GitHub. The dataset consists of over 10,000 overriding-overridden method-comment tuples, including method bodies, class names, and comments for both parent and child classes. Evaluation was conducted using a "cross-project" split to ensure the model's ability to generalize to entirely new codebases.
Results and Discussion
The HIERARCHY-AWARE SEQ2SEQ model significantly outperformed all baselines, including rule-based systems and previous deep learning models like DEEPCOM-HYBRID and the EDIT MODEL. Simply adding class hierarchy information led to an 80% improvement in performance over a standard Seq2Seq model that only looks at the method body.
Key Insights
- The Parent is the Blueprint: Ablation studies revealed that the superclass comment is the most critical piece of context; performance drops significantly without it.
- Specificity as a Choice: Treating specificity as a learnable latent variable allows the model to avoid generic "safe" responses (like "gets the value") in favor of precise technical descriptions (like "returns asn 1 encoded of the info access syntax").
Conclusion
By incorporating hierarchical context and specificity conditioning, the HIERARCHY-AWARE SEQ2SEQ model effectively captures the nuances of overriding methods. While currently focused on Java, the principles of leveraging class hierarchies are broadly applicable to other object-oriented languages. Future work may involve integrating these hierarchical features into larger pre-trained language models like CodeBERT to further improve linguistic fluidity.
Summary updated