Refactoring is an indispensable practice of improving the quality and maintainability of source code in software evolution. Rename refactoring is the most frequently performed refactoring that suggests a new name for an identifier to enhance readability when the identifier is poorly named. However, most existing works only identify renaming activities between two versions of source code, while few works express concern about how to suggest a new name. In this paper, we study automatic rename refactoring on variable names, which is considered more challenging than other rename refactoring activities. We first point out the connections between rename refactoring and various prevalent learning paradigms and the difference between rename refactoring and general text generation in natural language processing. Based on our observations, we propose RefBERT, a two-stage pre-trained framework for rename refactoring on variable names. RefBERT first predicts the number of sub-tokens in the new name and then generates sub-tokens accordingly. Several techniques, including constrained masked language modeling, contrastive learning, and the bag-of-tokens loss, are incorporated into RefBERT to tailor it for automatic rename refactoring on variable names. Through extensive experiments on our constructed refactoring datasets, we show that the generated variable names of RefBERT are more accurate and meaningful than those produced by the existing method.
翻译:重构是软件演化过程中改善源代码质量和可维护性不可或缺的实践。重命名重构是最常见的重构操作,当标识符命名不佳时,会为其建议新名称以增强可读性。然而,现有工作大多仅识别两个版本源代码之间的重命名活动,鲜有关注如何建议新名称。本文针对变量命名自动重命名重构展开研究,这被认为比其它重命名重构更具挑战性。我们首先揭示了重命名重构与多种主流学习范式之间的关联,并阐明了重命名重构与自然语言处理中通用文本生成的差异。基于上述观察,我们提出RefBERT——一种针对变量名重命名重构的两阶段预训练框架:该框架先预测新名称中子词元的数量,再据此生成子词元。通过融入带约束的掩码语言建模、对比学习以及词袋损失等多项技术,RefBERT被定制用于变量名的自动重命名重构。在自建的重构数据集上大量实验表明,相较于现有方法,RefBERT生成的变量名更加准确且富有意义。