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.
翻译:更简单且更直接的程序是增强可维护性、使编写安全无缺陷程序更容易的关键因素。然而,由于重构工作量大且存在破坏现有功能程序的风险,程序员往往不愿进行代码重构,这也导致潜在学习经验的损失。为缓解这一问题,我们展示了利用大语言模型GPT-3.5来为用户编写的Python程序提供更简洁版本的方案,旨在鼓励用户学习如何编写更好的程序。我们提出了一种方法,通过基于单示例提示的预评估,为每个目标编程问题选择最合适的代码重构示例,从而利用大语言模型的少样本示例提示。定量评估显示,对每个程序生成10个重构候选方案后,95.68%的程序可被成功重构,且仅保留语义正确的程序后,平均圈复杂度降低17.35%,平均代码行数减少25.84%。此外,定性评估表明该模型在代码格式化方面表现卓越,但同时也观察到删除或翻译注释等不必要的行为。