[论文解读] Refining ChatGPT-Generated Code: Characterizing and Mitigating Code Quality Issues
本文系统评估了4,066个由ChatGPT生成的Java和Python程序,覆盖2,033道LeetCode题目,以刻画代码质量问题、分析影响正确性的因素,并测试由静态分析和运行时反馈引导的自我修复提示。
We systematically study the quality of 4,066 ChatGPT-generated code implemented in two popular programming languages, i.e., Java and Python, for 2,033 programming tasks. The goal of this work is three folds. First, we analyze the correctness of ChatGPT on code generation tasks and uncover the factors that influence its effectiveness, including task difficulty, programming language, time that tasks are introduced, and program size. Second, we identify and characterize potential issues with the quality of ChatGPT-generated code. Last, we provide insights into how these issues can be mitigated. Experiments highlight that out of 4,066 programs generated by ChatGPT, 2,756 programs are deemed correct, 1,082 programs provide wrong outputs, and 177 programs contain compilation or runtime errors. Additionally, we further analyze other characteristics of the generated code through static analysis tools, such as code style and maintainability, and find that 1,930 ChatGPT-generated code snippets suffer from maintainability issues. Subsequently, we investigate ChatGPT's self-repairing ability and its interaction with static analysis tools to fix the errors uncovered in the previous step. Experiments suggest that ChatGPT can partially address these challenges, improving code quality by more than 20%, but there are still limitations and opportunities for improvement. Overall, our study provides valuable insights into the current limitations of ChatGPT and offers a roadmap for future research and development efforts to enhance the code generation capabilities of AI models like ChatGPT.
研究动机与目标
- 评估ChatGPT在不同任务难度、语言和任务年龄下的代码生成正确性。
- 利用静态分析和运行时数据描述ChatGPT生成代码中普遍存在的代码质量问题。
- 探索利用静态分析和运行时反馈来修复代码质量问题的提示策略。
提出的方法
- 构建一个包含2,033道LeetCode题目的时效性基准,提供Python和Java模板及公开测试用例。
- 对每道题目和语言使用ChatGPT生成代码(零-shot,温度参数为0)。
- 通过对LeetCode测试用例的pass@1来评估正确性。
- 应用静态分析工具(Python:Pylint、Flake8;Java:PMD、Checkstyle)对代码质量问题进行分类。
- 使用开放卡片排序将问题归纳为主题:编译/运行时错误、输出错误、代码风格/可维护性、以及性能/效率。
- 测试请求修复的提示,分别在有无静态分析/运行时反馈的情况下,评估自我修复能力。
实验结果
研究问题
- RQ1RQ1:在编程任务上的代码生成,ChatGPT的有效性如何?
- RQ2RQ2:ChatGPT生成的代码中常见的问题有哪些?
- RQ3RQ3:通过提示,ChatGPT是否能够修复代码质量问题?
主要发现
- Python生成的程序有66%是功能正确的(通过所有测试用例),Java生成的程序有69%是功能正确的。
- 即使在通过的代码中也存在代码质量问题,Java中有53%通过的代码存在风格/可维护性问题,Python中有37%通过的代码存在此类问题。
- ChatGPT可以在静态分析和运行时错误的反馈下部分修复问题,效果因语言和问题类型而异。
- 静态分析显示,47%的Java通过任务和63%的Python通过任务的代码较为干净,且随着任务难度的增加,整洁度下降。
- 总体而言,4,066个生成片段中有1,930个显示代码风格/可维护性问题,1,082个显示输出错误。
- 该研究为改进基于AI的代码生成提供了路线图,并发布数据集和复现实验包。
更好的研究,从现在开始
从论文设计到论文写作,大幅缩短您的研究时间。
无需绑定信用卡
本解读由 AI 生成,并经人工编辑审核。