Code smells indicate the potential problems of software quality so that developers can identify refactoring opportunities by detecting code smells. State-of-the-art approaches leverage heuristics, machine learning, and deep learning to detect code smells. However, existing approaches have not fully explored the potential of large language models (LLMs). In this paper, we propose \textit{PromptSmell}, a novel approach based on prompt learning for detecting multi-label code smell. Firstly, code snippets are acquired by traversing abstract syntax trees. Combined code snippets with natural language prompts and mask tokens, \textit{PromptSmell} constructs the input of LLMs. Secondly, to detect multi-label code smell, we leverage a label combination approach by converting a multi-label problem into a multi-classification problem. A customized answer space is added to the word list of pre-trained language models, and the probability distribution of intermediate answers is obtained by predicting the words at the mask positions. Finally, the intermediate answers are mapped to the target class labels by a verbalizer as the final classification result. We evaluate the effectiveness of \textit{PromptSmell} by answering six research questions. The experimental results demonstrate that \textit{PromptSmell} obtains an improvement of 11.17\% in $precision_{w}$ and 7.4\% in $F1_{w}$ compared to existing approaches.
翻译:代码坏味暗示了软件质量的潜在问题,使开发者能够通过检测代码坏味来识别重构机会。现有主流方法利用启发式算法、机器学习和深度学习技术检测代码坏味,但尚未充分探索大型语言模型的潜力。本文提出\textit{PromptSmell}——一种基于提示学习的多标签代码坏味检测新方法。首先,通过遍历抽象语法树获取代码片段,将代码片段与自然语言提示和掩码令牌结合,构建大型语言模型的输入。其次,为检测多标签代码坏味,我们采用标签组合方法将多标签问题转化为多分类问题:在预训练语言模型的词表中添加自定义答案空间,通过预测掩码位置的词汇获得中间答案的概率分布。最终通过言语化器将中间答案映射为目标类别标签,作为分类结果。我们通过回答六个研究问题评估\textit{PromptSmell}的有效性。实验结果表明,与现有方法相比,\textit{PromptSmell}在$precision_{w}$和$F1_{w}$上分别获得11.17%和7.4%的提升。