Bootstrapping Code-Text Pretrained Language Model to Detect Inconsistency Between Code and Comment

Anh T. V. Dau, Nghi D. Q. Bui, Jin L. C. Guo

Abstract

Comments within source code are essential for developers to comprehend the code's purpose and ensure its correct usage. However, as codebases evolve, maintaining an accurate alignment between the comments and the code becomes increasingly challenging. Recognizing the growing interest in automated solutions for detecting and correcting differences between code and its accompanying comments, current methods rely primarily on heuristic rules. In contrast, this paper presents DocChecker, a tool powered by deep learning. DocChecker is adept at identifying inconsistencies between code and comments, and it can also generate synthetic comments. This capability enables the tool to detect and correct instances where comments do not accurately reflect their corresponding code segments. We demonstrate the effectiveness of DocChecker using the Just-In-Time and CodeXGlue datasets in different settings. Particularly, DocChecker achieves a new State-of-the-art result of 72.3% accuracy on the Inconsistency Code-Comment Detection (ICCD) task and 33.64 BLEU-4 on the code summarization task against other Large Language Models (LLMs), even surpassing GPT 3.5 and CodeLlama. DocChecker is accessible for use and evaluation. It can be found on our GitHub https://github.com/FSoft-AI4Code/DocChecker and as an Online Tool http://4.193.50.237:5000/. For a more comprehensive understanding of its functionality, a demonstration video is available on YouTube https://youtu.be/FqnPmd531xw.

Introduction

In modern software engineering, code comments serve as a critical bridge between implementation logic and human understanding. However, they are prone to "documentation rot"—a phenomenon where code is updated but its corresponding documentation is not. This misalignment introduces technical debt and potential bugs. While Large Language Models (LLMs) have advanced code understanding, identifying the specific semantic nuances of why a comment is out of sync remains a challenge. DocChecker addresses this by moving beyond rigid heuristic rules to a deep-learning-based approach that both detects inconsistencies and suggests fixes.

Methodology: The Bootstrapped Approach

DocChecker utilizes an encoder-decoder framework based on the UniXcoder architecture (124M parameters). Its performance is driven by a multi-task pre-training strategy involving three core objectives:

  1. Code-Text Contrastive Learning (CTC): Aligns the feature spaces of code and natural language to recognize semantic relationships.
  2. Binary Classification (BC): Trains the model to explicitly distinguish between consistent and inconsistent pairs, hardening it against subtle mismatches.
  3. Text Generation (TG): Empowers the model to generate coherent summaries (comments) from code snippets.

By sharing weights across these tasks, DocChecker gains a holistic understanding of the interplay between code logic and descriptive text.

DocChecker UI demonstrating a consistent code-comment pair detection.

Evaluation and Datasets

The model was evaluated using two primary benchmarks:

  • CodeXGLUE: Used for pre-training across six programming languages and for the code summarization task.
  • Just-In-Time (JIT) Dataset: A collection of real-world "before and after" snapshots where developers updated comments alongside code changes, used for the Inconsistency Code-Comment Detection (ICCD) task.

Results and Discussion

DocChecker established a new state-of-the-art (SOTA) in inconsistency detection with 72.3% accuracy on the ICCD task. In code summarization, it achieved a 33.64 BLEU-4 score, significantly outperforming earlier models like RoBERTa and CodeBERT.

A key finding is that DocChecker’s specialized 124M-parameter architecture outperforms massive general-purpose models. In head-to-head tests on the JIT dataset, it surpassed CodeLlama (34B) by approximately 10% in accuracy. This demonstrates that targeted multi-task pre-training can be more effective than raw scale for specific software engineering tasks.

JSON output from DocChecker identifying an inconsistent comment and recommending a corrected version.

Conclusion

DocChecker provides a robust, deep-learning-based solution for maintaining documentation accuracy. By combining detection with generative correction, it offers a practical tool for developers to combat documentation rot. Future work includes expanding language support and integrating the tool directly into IDEs for real-time developer assistance.

Summary updated

BibTeX
@inproceedings{dau2024bootstrapping,
  author = {Anh T. V. Dau and Nghi D. Q. Bui and Jin L. C. Guo},
  title = {{Bootstrapping Code-Text Pretrained Language Model to Detect Inconsistency Between Code and Comment}},
  booktitle = {EACL (Demonstrations)},
  year = {2024},
  url = {https://openreview.net/forum?id=3jzArI9JZJ},
}
Figure