[论文解读] No Need to Lift a Finger Anymore? Assessing the Quality of Code Generation by ChatGPT
本研究系统评估了ChatGPT在C、C++、Java、Python和JavaScript五种编程语言中生成代码的正确性、复杂性和安全性。基于728道算法题和54个基于CWE的场景,研究发现,尽管ChatGPT在2021年以前的问题上生成的功能正确代码的接受率高出48.14%,其直接修复漏洞的能力较弱;然而,多轮修复可解决超过89%的漏洞,尽管复杂性通常增加,且非确定性影响输出的一致性。
Large language models (LLMs) have demonstrated impressive capabilities across various NLP tasks. Additionally, LLMs are also highly valuable in supporting software engineering tasks, particularly in the field of code generation. Automatic code generation is a process of automatically generating source code or executable code based on given specifications or requirements, improving developer productivity. In this study, we perform a systematic empirical assessment to the quality of code generation using ChatGPT. We leverage 728 algorithm problems in five languages (i.e., C, C++, Java, Python, and JavaScript) and 18 CWEs with 54 code scenarios for the code generation task. Our evaluation encompasses a comprehensive analysis of code snippets generated by ChatGPT, focusing on three critical aspects: correctness, complexity, and security. We also specifically investigate ChatGPT's ability to engage in multi-round fixing process (i.e., ChatGPT's dialog ability) of facilitating code generation. By delving into the generated code and examining the experimental results, this work provides valuable insights into the performance of ChatGPT in tackling code generation tasks over the three critical aspects. Overall, our findings uncover potential issues and limitations that arise in the ChatGPT-based code generation and lay the groundwork for improving AI and LLM-based code generation techniques.
研究动机与目标
- 评估ChatGPT在多种编程语言和问题集上生成代码的功能正确性。
- 分析ChatGPT生成代码片段的圈复杂度和认知复杂度。
- 调查在基于CWE的场景中生成代码中是否存在安全漏洞及其修复情况。
- 评估多轮修复过程在提升代码质量方面的有效性。
- 考察ChatGPT的非确定性对代码在正确性、复杂性和安全性方面一致性的影晌。
提出的方法
- 本研究使用来自编程竞赛平台的728道算法题,按时间划分为2021年以前和2021年以后的问题集。
- 在C、C++、Java、Python和JavaScript五种语言中,使用标准化的评判平台评估代码生成质量。
- 通过18个CWE在54个代码场景中评估安全漏洞,重点关注常见编码缺陷。
- 模拟多轮修复过程,即ChatGPT根据反馈迭代修改存在错误的代码。
- 为每个生成的代码片段计算圈复杂度和认知复杂度指标,以评估结构复杂度。
- 通过为同一提示生成多个代码样本并测量其在正确性、复杂度和安全性方面的差异,分析非确定性影响。
实验结果
研究问题
- RQ1在功能正确性方面,ChatGPT在2021年以前与2021年以后的算法问题上的代码生成表现有何差异?
- RQ2ChatGPT在不同编程语言中生成的代码的圈复杂度和认知复杂度分布如何?
- RQ3在真实世界的CWE场景中,ChatGPT生成的代码在多大程度上存在安全漏洞?
- RQ4多轮修复过程在纠正功能错误和解决生成代码中的安全漏洞方面有多有效?
- RQ5ChatGPT的非确定性如何影响其在正确性、复杂性和安全性方面代码生成的一致性?
主要发现
- 对于2021年以前的算法问题,ChatGPT在代码评判平台上的平均通过率比2021年以后的问题高出48.14%。
- 在直接将错误代码修正为功能正确代码方面,ChatGPT的多轮修复过程相对较弱。
- 圈复杂度和认知复杂度在不同编程语言中存在显著差异,且在多轮修复过程中复杂度通常保持不变或进一步增加。
- 在算法问题中,C、C++和Java的生成代码存在相关安全漏洞;在C和Python3的CWE场景中,生成代码也存在相关漏洞。
- 多轮修复过程成功修复了C语言中100%的漏洞,以及Python3中89.4%的漏洞,显示出强大的漏洞修复潜力。
- ChatGPT的非确定性导致同一提示生成的多个代码样本在功能正确性、复杂度和安全性方面存在差异。
更好的研究,从现在开始
从阅读论文到最终审阅,大幅缩短您的研究时间。
无需绑定信用卡
本解读由 AI 生成,并经人工编辑审核。