Towards Automating Code Review Activities

Rosalia Tufano, Luca Pascarella, Michele Tufano, Denys Poshyvanyk, Gabriele BavotaarXiv 2021

TL;DR

This paper presents a first step toward automating code review by training Transformer-based models to suggest code changes. The authors developed two models: one that helps contributors by predicting likely reviewer suggestions before submission, and another that helps reviewers by automatically generating code snippets based on their natural language comments.

What Problem Does This Paper Address?

Code review is a cornerstone of modern software engineering, proven to reduce bugs and improve code quality. However, it is notoriously time-consuming. Developers often spend over six hours a week reviewing code, and the process frequently forces costly context-switches.

While automation exists for catching syntax errors or simple linting issues, the more nuanced "human" part of code review—suggesting refactorings, fixing subtle logic bugs, or adhering to project-specific styles—remains largely manual. The authors aim to bridge this gap by using Deep Learning (DL) to learn from the millions of historical code reviews available in open-source and industrial repositories.

Methods

The authors treat code review as a "translation" task, similar to Neural Machine Translation (NMT). Their approach follows a multi-step pipeline:

  1. Mining: They crawled data from six Gerrit installations and over 2,500 GitHub Java repositories, collecting hundreds of thousands of review rounds.
  2. Abstraction: To handle the massive vocabulary of source code, they used a tool called src2abs. This replaces specific identifiers and literals with generic tokens (e.g., VAR_1, METHOD_2) while preserving Java keywords and common "idioms" (like i, 0, or size()). This process is illustrated in the abstraction example below.
Example of code abstraction
  1. Filtering: They applied strict filters to remove "noisy" comments (like "thanks!" or "looks good") that don't result in code changes. They even developed a keyword-based heuristic that achieved 93.4% precision in identifying relevant comments.
  2. Architectures: They trained two distinct Transformer models:
  3. 1-Encoder Model (Contributor Perspective): Takes the submitted code ($C_s$) as input and predicts the revised code ($C_r$).
  4. 2-Encoder Model (Reviewer Perspective): Takes both the submitted code ($C_s$) and the reviewer's natural language comment ($R_{nl}$) as inputs to generate the revised code ($C_r$).

The overall approach is summarized in Figure 1:

Fig. 1: Approach overview.

Datasets

The authors curated two primary datasets from their mining efforts:

  • $D_p$ (Pairs): 17,194 pairs of <submitted code, revised code> for the 1-encoder model.
  • $D_t$ (Triplets): 17,194 triplets of <submitted code, comment, revised code> for the 2-encoder model.

Both datasets were split into 80% training, 10% evaluation, and 10% testing.

Key Insights and Ideas

  • Context is King: The most significant finding is how much the natural language comment helps. Adding the reviewer's comment as a second input (the 2-encoder model) roughly quadrupled the model's ability to produce a "perfect" code match at a beam size of 1.
  • Abstraction as a Double-Edged Sword: While abstraction makes training feasible by limiting vocabulary, it prevents the model from suggesting changes that require entirely new identifiers not already present in the code or the list of common idioms.
  • Refactoring vs. Behavioral Changes: The models aren't just doing simple text replacement. Qualitative analysis showed they could handle both refactorings (like changing method visibility or simplifying return statements) and behavioral changes (like adding negations to if conditions or modifying parameters in method calls).

Key Results

The authors evaluated their models using "perfect predictions" (an exact match with what the developer actually wrote), BLEU-4 scores, and Levenshtein distance.

TABLE II: Quantitative results: Perfect predictions, BLEU-4, and Levenshtein distance achieved by the models
  • Contributor Side (1-Encoder): With a beam search size of 10 (generating 10 candidates), the model correctly predicted the developer's exact change in 15.76% of cases.
  • Reviewer Side (2-Encoder): With the same beam size of 10, the model succeeded in 30.72% of cases.
  • Qualitative Success: Even when the model didn't provide a "perfect" match, the authors found that many suggestions were semantically equivalent or still highly meaningful to a developer.

Limitations and Future Work

  • Granularity: The current work is limited to method-level changes. Real-world reviews often span multiple methods or files.
  • Vocabulary: The reliance on abstraction means the model cannot "invent" new variable names.
  • Language Scope: The study focused exclusively on Java.
  • Future Directions: The authors suggest exploring different DL architectures (like Graph Neural Networks to better capture code structure) and increasing the training data volume to cover a wider variety of complex code transformations.

Summary updated

BibTeX
@misc{tufano2021towards,
  author = {Rosalia Tufano and Luca Pascarella and Michele Tufano and Denys Poshyvanyk and Gabriele Bavota},
  title = {{Towards Automating Code Review Activities}},
  year = {2021},
  eprint = {2101.02518},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2101.02518},
}
Figure