Automating code review activities by large-scale pre-training

Zhiyu Li, Shuai Lu, Daya Guo, Nan Duan, Shailesh Jannu, Grant Jenks, Deep Majumder, Jared Green, Alexey Svyatkovskiy, Shengyu Fu, Neel Sundaresan

TL;DR

This paper introduces CodeReviewer, a novel pre-trained model designed to automate various code review tasks. By leveraging a large-scale, multilingual dataset of code changes and reviews, and introducing specialized pre-training tasks, CodeReviewer significantly outperforms existing models in code quality estimation, review comment generation, and code refinement.

What Problem Does This Paper Address?

Code review is a crucial but time-consuming part of the software development lifecycle. Developers spend a significant amount of effort understanding code changes, identifying potential issues, and providing feedback. While existing research has explored automating aspects of code review, many approaches fail to adequately consider the unique nature of code changes (diffs) or leverage the vast amount of real-world code review data available. This paper addresses the need for more effective automation by developing a model that can better understand code diffs and generate relevant feedback, ultimately aiming to reduce the burden on human reviewers.

Methods

The core of this work is the CodeReviewer model, an encoder-decoder Transformer architecture similar to T5. What makes it novel are:

  • Input Representation: Unlike previous models that might process raw code, CodeReviewer is designed to directly ingest code diffs. This is crucial because review comments and quality assessments are inherently tied to the changes made, not just the entire codebase. Special tokens are used to represent added, deleted, and unchanged lines.
  • Specialized Pre-training Tasks: To equip the model with code review-specific understanding, four pre-training tasks are introduced:
  • Diff Tag Prediction (DTP): Helps the model understand the meaning of line tags ([ADD], [DEL], [KEEP]) in diffs.
  • Denoising Code Diff (DCD): Corrupts code diffs by masking lines and trains the model to reconstruct them, improving understanding of code changes.
  • Denoising Review Comment (DRC): Corrupts review comments and trains the model to reconstruct them, improving its ability to understand and generate natural language feedback.
  • Review Comment Generation (RCG): A bi-modal task where the model learns to generate review comments given code changes, directly addressing the relationship between code and feedback.
  • Multilingual Dataset: The model is pre-trained on a large, diverse dataset spanning nine popular programming languages, aiming for broader applicability.

Datasets

The authors introduce a new, large-scale CodeReview dataset collected from GitHub pull requests across nine programming languages. This dataset is used for both pre-training and creating benchmark datasets for three downstream tasks:

  • Code Change Quality Estimation: Distinguishing between code changes that need review comments and those that don't.
  • Review Comment Generation: Generating natural language feedback for code changes.
  • Code Refinement: Revising code based on review comments.

The paper highlights that this dataset is the largest and most comprehensive of its kind, covering code changes and corresponding review comments.

Key Insights and Ideas

  • Code Diffs are Key: The paper strongly emphasizes that treating code diffs as the primary input for code review tasks is critical for model performance, as it directly reflects the focus of human reviewers.
  • Specialized Pre-training Matters: The proposed pre-training tasks (DTP, DCD, DRC, RCG) are shown to be more effective for code review automation than generic pre-training objectives. This suggests that tailoring pre-training to the specific domain is highly beneficial.
  • Multilingualism Boosts Performance: Training on a diverse set of programming languages not only improves generalization but also enhances performance on individual languages, even those considered "low-resource."
  • Human Evaluation is Crucial: For tasks like review comment generation, automatic metrics like BLEU can be misleading. Human evaluation revealed that CodeReviewer's generated comments were more informative and relevant, even if BLEU scores were modest.

Key Results

  • Code Change Quality Estimation: CodeReviewer achieved the highest accuracy, precision, recall, and F1 scores among all evaluated models, outperforming T5 and CodeT5 by a significant margin (e.g., ~7-8% improvement in F1 and accuracy over T5).
  • Review Comment Generation: While BLEU scores were modest (indicating the task's difficulty), human evaluation showed CodeReviewer significantly improved information and relevance scores by approximately 20% compared to baselines.
  • Code Refinement: CodeReviewer achieved an Exact Match (EM) rate of over 30%, more than double that of T5 and 25% higher than CodeT5, demonstrating its superior ability to generate correct code revisions.
  • Ablation Studies: Removing individual pre-training tasks (DTP, DCD, CMT) led to performance drops, highlighting the importance of each component, with DTP and DCD being particularly crucial for understanding code diffs.

Limitations and Future Work

  • Dataset Bias: The dataset is collected from open-source projects, which may not fully represent industrial code review practices. Additionally, the current approach considers only a single review comment per code change, while real-world reviews can involve multiple comments and reviewers.
  • Hyperparameter Tuning: The authors acknowledge that more extensive hyperparameter tuning could potentially yield further improvements.
  • Future Work: The paper suggests exploring more complex interactions between multiple comments and multiple code changes, and further investigating the benefits of multilingual pre-training across a wider range of tasks.

Summary updated

BibTeX
@inproceedings{li2022automating,
  author = {Zhiyu Li and Shuai Lu and Daya Guo and Nan Duan and Shailesh Jannu and Grant Jenks and Deep Majumder and Jared Green and Alexey Svyatkovskiy and Shengyu Fu and Neel Sundaresan},
  title = {{Automating code review activities by large-scale pre-training}},
  booktitle = {ESEC/SIGSOFT FSE},
  year = {2022},
  doi = {10.1145/3540250.3549081},
}
Figure