TL;DR
This paper investigates whether Neural Machine Translation (NMT) can learn to automatically perform non-trivial code changes, such as refactorings and bug fixes, by training on thousands of real-world pull requests. The authors demonstrate that a standard NMT architecture can perfectly replicate developer-implemented changes in up to 36% of cases for small-to-medium methods, successfully learning a diverse taxonomy of meaningful code transformations.
What Problem Does This Paper Address?
While Deep Learning (DL) has shown promise in automating simple software engineering tasks—like generating comments or predicting defects—the "holy grail" remains the automation of non-trivial coding activities. We know DL can help find bugs, but can it learn the nuanced patterns required to fix them or to refactor messy code into something cleaner?
The primary challenge is the "open vocabulary" problem: source code contains a virtually infinite number of unique identifiers and literals, making it difficult for standard models to generalize. Furthermore, there has been a lack of qualitative evidence regarding exactly what kinds of logic changes these models are capable of learning. This paper bridges that gap by evaluating NMT's ability to "translate" a method from its state before a pull request (PR) to its state after the PR is merged.
Methods
The authors treat code evolution as a translation task. Their pipeline consists of four main stages:
- Mining and Extraction: They crawled three large Gerrit repositories, extracting pairs of methods (pre-PR and post-PR) from merged and reviewed pull requests.
- Code Abstraction: To handle the massive vocabulary of source code, they implemented a sophisticated abstraction process. They kept the top 300 most frequent identifiers and literals (like common API calls or
0and1) but replaced all other unique names with reusable IDs (e.g.,VAR_0,METHOD_1). This reduced the vocabulary from over 370,000 unique tokens to just 740. - NMT Training: They used a standard RNN Encoder-Decoder architecture with an attention mechanism. The model learns the conditional probability of a "post-PR" sequence given a "pre-PR" sequence.
- Beam Search & Concretization: During testing, the model uses beam search to generate the top $k$ most likely code transformations. These abstracted sequences are then "concretized" by mapping the IDs back to their original names using a saved map.
Datasets
The study utilized three major open-source ecosystems hosted on Gerrit: Android, Google Source, and Ovirt.
After filtering for Java methods and removing duplicates, they partitioned the data into two main categories based on token count:
- $M_{small}$: Methods with $\le$ 50 tokens.
- $M_{medium}$: Methods with 50–100 tokens.
Key Insights and Ideas
- Code Change as Translation: The core "aha" moment is that pull requests represent a high-quality "parallel corpus" for code. By focusing on merged and reviewed PRs, the authors ensure the model learns "meaningful" changes rather than random churn.
- Abstraction is Essential: By stripping away specific names while preserving the structural relationships (via IDs), the model can learn the logic of a change (e.g., "add a null check for the first parameter") rather than getting bogged down by the specific name of that parameter.
- Transfer Learning Potential: The model trained on the combined "All" dataset outperformed models trained on individual datasets. This suggests that developers across different projects follow similar patterns when fixing bugs or refactoring code.
Key Results
The quantitative results were surprisingly strong for a first step in this direction. When the model was allowed to provide 10 candidate guesses (beam size 10), it perfectly matched the developer's manual change in 36% of cases for small methods and 30% for medium methods.
Qualitatively, the authors developed a detailed taxonomy of the changes the model successfully learned, categorized into Bug Fixes, Refactorings, and Other changes.
Specific examples of learned transformations include:
- Refactoring: Converting anonymous classes to Java 8 lambda expressions and broadening method visibility.
- Bug Fixing: Adding null checks before method calls and wrapping resource allocations in
try-with-resourcesblocks to prevent leakages.
Limitations and Future Work
- Context Window: The study was limited to methods under 100 tokens. Longer methods represent a "long tail" in the distribution that current NMT models struggle to process efficiently.
- New Features: The model primarily learned refactorings and bug fixes. It did not learn how to implement entirely new features, likely because new features require a broader understanding of the entire codebase, not just a single method.
- Language Support: The current infrastructure is specific to Java, though the authors note the underlying NMT approach is language-agnostic.
- Future Direction: The authors suggest that future work could leverage the rich qualitative data in code reviews (the "why" behind a change) to help the model justify its suggested transformations.
Summary updated