Refactoring Programs Using Large Language Models with Few-Shot Examples
作者: Atsushi Shirafuji, Yusuke Oda, Jun Suzuki, Makoto Morishita, Yutaka Watanobe
分类: cs.PL, cs.AI, cs.CL, cs.SE
发布日期: 2023-11-20
备注: 10 pages, 10 figures, accepted to the 30th Asia-Pacific Software Engineering Conference (APSEC 2023)
DOI: 10.1109/APSEC60848.2023.00025
💡 一句话要点
利用大型语言模型进行代码重构以提升程序可维护性
🎯 匹配领域: 支柱九:具身大模型 (Embodied Foundation Models)
关键词: 代码重构 大型语言模型 GPT-3.5 程序可维护性 机器学习 自动化编程 软件开发
📋 核心要点
- 现有代码重构方法面临程序员因工作量大和风险高而不愿意进行重构的挑战,导致潜在学习机会的丧失。
- 本文提出利用大型语言模型GPT-3.5,通过少量示例提示用户编写更简洁的代码,从而鼓励学习更好的编程实践。
- 实验结果表明,95.68%的程序可成功重构,圈复杂度平均降低17.35%,行数减少25.84%,显示出显著的性能提升。
📝 摘要(中文)
程序的复杂性直接影响其可维护性和安全性,但程序员往往因工作量大和潜在风险而不愿进行代码重构。为此,本文展示了如何利用大型语言模型(LLM)GPT-3.5,建议用户编写更简洁的Python程序,以促进学习更好的编程方法。我们提出了一种通过少量示例进行提示的方法,基于先前的评估选择最适合的代码重构示例。定量评估显示,95.68%的程序可以通过生成10个候选版本进行重构,平均圈复杂度降低17.35%,平均行数减少25.84%。定性评估则显示出在代码格式化方面的卓越能力,同时也观察到删除或翻译注释等不必要的行为。
🔬 方法详解
问题定义:本文旨在解决程序员因重构工作量大和风险高而不愿进行代码重构的问题,导致程序复杂性高和可维护性差。
核心思路:通过利用大型语言模型(LLM)GPT-3.5,结合少量示例的提示,自动生成更简洁的代码版本,以降低程序复杂性并促进学习。
技术框架:整体流程包括:首先对用户编写的Python程序进行分析,然后通过LLM生成多个候选重构版本,最后根据语义正确性筛选出最佳版本。
关键创新:本研究的创新点在于通过少量示例的提示选择最适合的重构示例,显著提高了重构的成功率和代码质量,与传统重构方法相比,降低了程序员的负担。
关键设计:在参数设置上,选择了GPT-3.5作为基础模型,并通过评估不同的提示策略来优化生成结果,确保生成的代码在语义上是正确的。
🖼️ 关键图片
📊 实验亮点
实验结果显示,95.68%的程序能够成功重构,生成的候选版本中,平均圈复杂度降低了17.35%,平均行数减少了25.84%。这些结果表明,利用大型语言模型进行代码重构在提升代码质量和可维护性方面具有显著效果。
🎯 应用场景
该研究的潜在应用领域包括软件开发、教育和代码审查等。通过自动化代码重构,程序员可以更高效地维护和优化代码,减少人为错误,同时促进新手程序员的学习和成长。未来,该技术可能会在更广泛的编程语言和开发环境中得到应用,进一步提升软件开发的效率和质量。
📄 摘要(原文)
A less complex and more straightforward program is a crucial factor that enhances its maintainability and makes writing secure and bug-free programs easier. However, due to its heavy workload and the risks of breaking the working programs, programmers are reluctant to do code refactoring, and thus, it also causes the loss of potential learning experiences. To mitigate this, we demonstrate the application of using a large language model (LLM), GPT-3.5, to suggest less complex versions of the user-written Python program, aiming to encourage users to learn how to write better programs. We propose a method to leverage the prompting with few-shot examples of the LLM by selecting the best-suited code refactoring examples for each target programming problem based on the prior evaluation of prompting with the one-shot example. The quantitative evaluation shows that 95.68% of programs can be refactored by generating 10 candidates each, resulting in a 17.35% reduction in the average cyclomatic complexity and a 25.84% decrease in the average number of lines after filtering only generated programs that are semantically correct. Furthermore, the qualitative evaluation shows outstanding capability in code formatting, while unnecessary behaviors such as deleting or translating comments are also observed.