arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2311.11690cs.PLcs.AIcs.CLcs.SE
收藏

利用少样本示例与大型语言模型重构程序

Refactoring Programs Using Large Language Models with Few-Shot Examples

University of Aizu(会津大学);Tohoku University(东北大学);NTT Communication Science Laboratories(NTT通信科学实验室)

Atsushi Shirafuji, Yusuke Oda, Jun Suzuki, Makoto Morishita, Yutaka Watanobe

AI总结:

本文提出用GPT-3.5并依据单样本评估为Python程序选择适配的少样本重构示例,实验表明可在保证语义正确的同时显著降低复杂度与代码行数。

摘要:

复杂度更低、更直白的程序是提升其可维护性的关键因素,也使编写安全且无缺陷的程序更加容易。然而,由于工作量沉重且存在破坏可运行程序的风险,程序员不愿进行代码重构,这也导致潜在学习经验的流失。为缓解该问题,我们展示了使用大型语言模型(LLM)GPT-3.5来建议用户所写Python程序的更低复杂度版本,旨在鼓励用户学习如何编写更好的程序。我们提出一种利用LLM少样本示例提示的方法:基于先前对单样本示例提示的评估,为每个目标编程问题选择最适合的代码重构示例。定量评估表明,在仅筛选语义正确的生成程序后,通过为每个程序生成10个候选,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.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑