基于LLM的代码重构:结合少样本设置的全面评估
Code Refactoring with LLM: A Comprehensive Evaluation With Few-Shot Settings
AI总结:
本研究提出基于LLM的多语言代码重构框架,通过少样本学习和提示工程提升重构准确性与效率,实验显示Java在正确性与可编译性上表现最佳。
摘要:
在当今世界,程序员的注意力已从编写复杂且容易出错的代码转向优先考虑简单、清晰、高效且可持续的代码,使程序更容易理解。代码重构在这一转变中起着关键作用,通过改进结构组织并优化性能。然而,现有重构方法在跨多种编程语言和编码风格的泛化能力有限,因为它们通常依赖于手动编写的转换规则。本研究的目标是(i)开发一个基于大语言模型(LLM)的框架,能够在多种语言(C、C++、C#、Python、Java)中进行准确且高效的代码重构;(ii)调查提示工程(温度、不同样本算法)和指令微调对重构效果的影响;(iii)通过实证指标和人类评估评估重构代码的质量改进(可编译性、正确性、距离、相似性、行数、令牌、字符、环形复杂度)。为了实现这些目标,我们提出了一种结合微调提示工程和少样本学习的模型用于多语言代码重构。实验结果表明,Java在10样本设置下实现了高达99.99%的总体正确性,记录了比原始源代码最高的平均可编译性94.78%,并保持了较高的相似性(约53-54%),从而在结构修改和语义保留之间表现出良好的平衡。Python在所有样本中展现出最低的结构距离(约277-294)的同时实现了中等相似性(约44-48%),这表明了一致且最小干扰的重构。
英文摘要:
In today's world, the focus of programmers has shifted from writing complex, error-prone code to prioritizing simple, clear, efficient, and sustainable code that makes programs easier to understand. Code refactoring plays a critical role in this transition by improving structural organization and optimizing performance. However, existing refactoring methods are limited in their ability to generalize across multiple programming languages and coding styles, as they often rely on manually crafted transformation rules. The objectives of this study are to (i) develop an Large Language Models (LLMs)-based framework capable of performing accurate and efficient code refactoring across multiple languages (C, C++, C#, Python, Java), (ii) investigate the impact of prompt engineering (Temperature, Different shot algorithm) and instruction fine-tuning on refactoring effectiveness, and (iii) evaluate the quality improvements (Compilability, Correctness, Distance, Similarity, Number of Lines, Token, Character, Cyclomatic Complexity) in refactored code through empirical metrics and human assessment. To accomplish these goals, we propose a fine-tuned prompt-engineering-based model combined with few-shot learning for multilingual code refactoring. Experimental results indicate that Java achieves the highest overall correctness up to 99.99% the 10-shot setting, records the highest average compilability of 94.78% compared to the original source code and maintains high similarity (Approx. 53-54%) and thus demonstrates a strong balance between structural modifications and semantic preservation. Python exhibits the lowest structural distance across all shots (Approx. 277-294) while achieving moderate similarity ( Approx. 44-48%) that indicates consistent and minimally disruptive refactoring.
机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。