TL;DR
This paper dives deep into the quality of code generated by ChatGPT, finding that while it's functional for many tasks, it often suffers from bugs, style issues, and incorrect outputs. The study also explores how ChatGPT can fix these issues with targeted feedback, showing promise but also highlighting areas for improvement.
What Problem Does This Paper Address?
The explosive popularity of ChatGPT has led to its widespread adoption for code generation, even by those without extensive programming experience. However, the reliability and quality of this AI-generated code remain largely unexamined. This poses a significant risk, as code quality issues can lead to bugs, security vulnerabilities, and difficulties in understanding and maintaining software. This paper aims to systematically investigate the quality of ChatGPT-generated code, identify common issues, and explore methods for mitigating them, thereby informing the responsible use and further development of AI code generation tools.
Methods
The researchers employed a multi-faceted approach:
- Data Collection: They curated a dataset of 2,033 programming tasks from LeetCode, covering various difficulty levels and release dates.
- Code Generation: ChatGPT was prompted to generate solutions for these tasks in both Java and Python using a zero-shot approach (no examples provided in the prompt).
- Performance Evaluation (RQ1): The generated code was evaluated against LeetCode's test suites to determine its functional correctness (pass@1 rate). Factors like task difficulty, time of introduction, and program length were analyzed for their impact on performance.
- Code Quality Analysis (RQ2): A combination of compiler outputs (runtime errors) and static analysis tools (Pylint, Flake8 for Python; PMD, Checkstyle for Java) were used to identify and categorize code quality issues. An open card sorting method was used to group these issues into meaningful categories.
- Self-Repair Investigation (RQ3): ChatGPT's ability to fix identified code quality issues was tested using two types of prompts: "simple feedback" (general notification of issues) and "feedback with static analysis and runtime errors" (detailed information about specific problems).
Datasets
- LeetCode Dataset: A newly constructed dataset comprising 2,033 programming tasks from LeetCode. This dataset is notable for its temporal diversity, including tasks released up to March 2023, and its coverage of both Java and Python. It's publicly available for replication.
- Existing Benchmarks: The paper references existing benchmarks like HumanEval and MBPP but highlights their limitations (outdated, limited temporal metadata) for evaluating modern LLMs.
Key Insights and Ideas
- Task Difficulty is a Major Hurdle: ChatGPT's performance significantly degrades as task difficulty increases, especially for newer, more complex problems.
- Time Matters: Performance drops noticeably for tasks introduced after ChatGPT's training data cutoff (September 2021), suggesting limitations in adapting to novel challenges.
- Code Style is a Persistent Problem: Even when code passes tests, a substantial portion suffers from code style and maintainability issues. This is a more common problem than outright functional errors.
- Language Differences: Python and Java exhibit distinct compilation and runtime error patterns, and their top code style issues are language-specific, requiring tailored solutions.
- Feedback is Key for Repair: ChatGPT can partially fix code quality issues, but the effectiveness of its self-repair capabilities is highly dependent on the quality and specificity of the feedback provided. Detailed feedback, especially from static analysis tools, is generally more effective.
- Self-Repair Can Introduce New Issues: While ChatGPT can fix existing problems, its repair process can sometimes introduce new bugs or quality concerns, underscoring the need for careful validation.
Key Results
- Functional Correctness: Out of 4,066 generated programs, 2,756 (67.8%) were deemed correct (passed all test cases).
- Error Breakdown: 1,082 programs (26.6%) produced wrong outputs, and 177 (4.3%) had compilation or runtime errors.
- Code Style Issues: 1,930 programs (47.5%) exhibited code style and maintainability issues.
- Performance Decline: ChatGPT's pass@1 rate for hard tasks was significantly lower than for easy tasks. For Python, it dropped from 0.890 (easy) to 0.400 (hard).
- Temporal Impact: Performance on tasks introduced after September 2021 was notably lower.
- Self-Repair Effectiveness: ChatGPT could resolve 20% to 60% of code quality issues. For code style and maintainability, it achieved over 60% with detailed feedback.
- Iterative Improvement: Iterative feedback rounds showed a clear increase in pass rates and code quality, stabilizing after a few iterations.
Limitations and Future Work
- External Validity: The study's reliance on LeetCode tasks and only Java/Python might limit generalizability to other domains or languages. Future work could expand the dataset and consider different project types.
- Test Suite Completeness: While LeetCode's test suites are robust, they might not catch all potential bugs.
- Prompt Engineering: The study acknowledges that prompt engineering significantly influences generated code quality. Future work should focus on optimizing prompts for better accuracy and reliability.
- Introducing New Issues: The self-repair process can inadvertently introduce new problems, suggesting a need for more precise feedback mechanisms and better strategies to mitigate this.
- Model Evolution: The study used a specific version of ChatGPT (March 2023, trained on data up to 2021). Future research should consider newer models and their evolving capabilities.
Summary updated