Abstract
Maintaining consistency between source code and its documentation is a critical challenge in software engineering. This paper explores the application of modern Transformer architectures, specifically BERT and Longformer, to the task of detecting "stale" code comments. By framing the problem as a Natural Language Inference (NLI) task, the authors demonstrate that general-purpose language models can effectively identify inconsistencies without the need for manual feature engineering. Our findings show that the extended context window of the Longformer significantly improves detection performance by capturing method logic that often falls outside the standard 512-token limit of BERT.
Introduction: The Cost of Stale Comments
Software developers spend approximately 82% of their time on program comprehension. High-quality comments serve as the primary roadmap for this process. However, unlike code, comments are not executable and are frequently neglected during refactoring. When a developer updates a function's logic but fails to update the associated Javadoc, the comment becomes a liability—a source of technical debt that leads to developer confusion and potential bugs.
Existing detection tools often rely on rigid, rule-based systems or require extensive manual feature engineering, such as extracting parts-of-speech or code-overlap metrics. This research investigates whether general-purpose language models can learn these complex relationships automatically.
Methodology: NLI for Code Consistency
The authors treat the problem as Natural Language Inference (NLI). In standard NLP, NLI determines if a "premise" entails or contradicts a "hypothesis." In this context, the code comment serves as the premise, and the code method itself serves as the hypothesis. The model must determine if the code implementation supports the claims made in the comment.
Two primary architectures were evaluated:
- BERT: The industry standard for NLP, limited to a 512-token sequence length.
- Longformer: A variant designed for long documents, configured here to handle up to 1,024 tokens. This is crucial because code methods are often significantly longer than standard natural language sentences.
The models were tested in two scenarios: Post hoc (detecting existing inconsistencies) and Just-in-time (JIT) (detecting if a recent code change rendered an existing comment obsolete).
Datasets and Experimental Setup
The study utilized a dataset curated by Panthaplackel et al. (2021), consisting of over 40,000 examples of Javadoc comments (@return, @param, and general summaries) paired with Java methods from popular open-source projects. To aid the model in JIT detection, the data includes "edit sequences" ($M_{edit}$), which represent the specific delta between the old and new versions of a function.
Results and Discussion
The Importance of Context Length
Code is inherently verbose. The authors found that BERT’s 512-token limit often forced the model to truncate the end of a function—exactly where return statements and critical final logic typically reside. By doubling the sequence length to 1,024 tokens, the Longformer achieved a massive jump in performance for post hoc detection, reaching an F1 score of 86.4 and 92.7% precision.
Simplicity and Performance
One of the most significant findings is that these "vanilla" Transformer models achieved state-of-the-art-comparable results without using external linguistic features like part-of-speech tags. This suggests that the pre-trained weights of BERT and Longformer already capture the necessary "common sense" regarding code-comment relationships. Additionally, the authors noted that swapping the input order to (Comment, Code) improved results by ensuring the shorter comment was never truncated by the model's length limits.
Conclusion and Future Work
While BERT and Longformer prove highly competitive for inconsistency detection, challenges remain. A puzzling performance drop for Longformer in JIT settings compared to post hoc settings suggests that the model's handling of code deltas requires further refinement. The ultimate goal is to move beyond mere detection toward Automatic Comment Updating, using sequence-to-sequence models to suggest corrected documentation immediately after code is edited.
Summary updated