RefBERT: A Two-Stage Pre-trained Framework for Automatic Rename Refactoring

Hao Liu, Yanlin Wang, Zhao Wei, Yong Xu, Juhong Wang, Hui Li, Rongrong Ji · DOI · OpenReview

TL;DR

This paper introduces REFBERT, a novel two-stage framework for automatically suggesting better names for variables in code. By combining pre-training on large code corpora with a specialized fine-tuning approach that considers both token order flexibility and semantic meaning, REFBERT significantly outperforms existing methods in generating accurate and meaningful variable names.

What Problem Does This Paper Address?

Software development involves frequent code refactoring to improve readability and maintainability. Renaming identifiers, especially variables, is a common and crucial refactoring task. However, suggesting a good, descriptive name for a variable is challenging, requiring deep understanding of the code's context. While many tools can detect refactoring, few can automatically suggest appropriate new names. Existing approaches often struggle with the nuances of variable naming, which differ from general text generation due to the less strict ordering of sub-tokens and the need for context-specific meaning. This paper aims to bridge this gap by developing a robust framework for automatic rename refactoring specifically for variable names.

Methods

REFBERT employs a two-stage approach:

  1. Length Prediction (LP) Task: The first stage predicts the number of sub-tokens that will constitute the new variable name. This is crucial because variable names can be composed of multiple parts (e.g., userCount has two tokens: user and count). This stage uses a standard cross-entropy loss.
  2. Token Generation (TG) Task: The second stage generates the actual sub-tokens for the new variable name, guided by the predicted length. Here, the authors introduce several key innovations:
  3. Constrained Masked Language Modeling (cMLM): Similar to BERT's pre-training, but specifically masks the tokens of the target variable name.
  4. Contrastive Learning: This technique helps the model learn to generate names that are semantically close to the desired refactored name and far from the original, poorly named variable.
  5. Bag-of-Tokens (BoT) Loss: Unlike standard cross-entropy loss, which penalizes incorrect token order heavily, the BoT loss focuses on the presence of correct tokens, regardless of their order. This is vital because variable names like versionDefault and defaultVersion convey similar meanings but have different token orders. This loss helps generate more meaningful and abbreviated names.

REFBERT is pre-trained on large code corpora (like CODESEARCHNET) to gain general programming language understanding, and then fine-tuned on specific rename refactoring datasets.

Datasets

The paper utilizes and constructs datasets for training and evaluation:

  • Pre-training:
    • CODESEARCHNET: A large dataset of code snippets from GitHub across multiple programming languages.
  • CODE2SEQ (JAVA-SMALL): A dataset derived from Java projects, also used for pre-training.
  • Fine-tuning/Evaluation:
    • TL-CODESUM: An existing code summarization dataset adapted for rename refactoring by assuming existing variable names are well-formed and then artificially creating "before" and "after" states.
  • JAVAREF: A newly constructed dataset specifically for rename refactoring. It was built by collecting commit data from Java open-source projects, using refactoring detection tools, and then manually verifying and preprocessing the data.

Key Insights and Ideas

  • Rename Refactoring is Not Just Text Generation: The authors highlight that standard NLP generation techniques (like strict cross-entropy loss) are suboptimal for rename refactoring because variable name sub-tokens don't always need a strict order to maintain meaning.
  • The Power of Context and Flexibility: REFBERT's success stems from its ability to leverage code context (via pre-training and cMLM) and its flexibility in handling token order (via BoT loss) and semantic similarity (via contrastive learning).
  • Two-Stage Approach is Effective: Separating the prediction of the number of tokens from the generation of tokens simplifies the problem and improves overall performance.
  • Data Sparsity is a Major Hurdle: The paper acknowledges that variable names are highly sparse, making it difficult for models to learn good representations. Their approach, particularly the use of sub-tokenization and pre-training, aims to mitigate this.

Key Results

  • LP Task: REFBERT significantly outperforms a heuristic-based baseline in predicting the correct number of tokens for refactored variable names, achieving high Hit@1 and Hit@3 scores (e.g., ~0.72 Hit@1 on JAVAREF).
  • TG Task: REFBERT substantially outperforms the NATURALIZE baseline in generating accurate and meaningful variable names. For instance, on JAVAREF, REFBERT achieves an Accuracy of ~0.581 and an Exact Match (EM) of ~0.524, compared to NATURALIZE's ~0.035 Accuracy and ~0.029 EM.
  • Ablation Study: The results show that each component of REFBERT (BoT loss, contrastive learning, cMLM) contributes positively to its performance. Removing any of these components leads to a degradation in results.
  • Qualitative Improvements: Case studies demonstrate that REFBERT with the BoT loss can generate more meaningful, abbreviated, and non-repetitive variable names compared to using only cross-entropy loss.

Limitations and Future Work

  • Oracle Bias and False Oracles: The authors acknowledge potential biases in their constructed JAVAREF dataset due to the reliance on automated refactoring detection tools and assumptions made when adapting TL-CODESUM. They plan to continue improving JAVAREF to mitigate these issues.
  • Metric Limitations: The paper notes that there isn't a universally agreed-upon metric for evaluating rename refactoring, and suggests considering multiple metrics for a comprehensive assessment.
  • Future Work: The authors plan to explore better methods for determining the order of generated tokens and continue enhancing their refactoring dataset.

Summary updated

BibTeX
@inproceedings{liu2023refbert,
  author = {Hao Liu and Yanlin Wang and Zhao Wei and Yong Xu and Juhong Wang and Hui Li and Rongrong Ji},
  title = {{RefBERT: A Two-Stage Pre-trained Framework for Automatic Rename Refactoring}},
  booktitle = {ISSTA},
  year = {2023},
  doi = {10.1145/3597926.3598092},
}
Figure