[Paper Review] An Empirical Study on the Code Refactoring Capability of Large Language Models
This empirical study evaluates StarCoder2's code refactoring capabilities against human developers across 30 open-source Java projects, finding that the model reduces code smells 20.1% more effectively than developers in automated refactoring tasks. It demonstrates that one-shot prompting and generating multiple refactorings per input significantly improve performance, with a 28.8% higher unit test pass rate when generating five refactorings compared to one.
Large Language Models (LLMs) have shown potential to enhance software development through automated code generation and refactoring, reducing development time and improving code quality. This study empirically evaluates StarCoder2, an LLM optimized for code generation, in refactoring code across 30 open-source Java projects. We compare StarCoder2's performance against human developers, focusing on (1) code quality improvements, (2) types and effectiveness of refactorings, and (3) enhancements through one-shot and chain-of-thought prompting. Our results indicate that StarCoder2 reduces code smells by 20.1% more than developers, excelling in systematic issues like Long Statement and Magic Number, while developers handle complex, context-dependent issues better. One-shot prompting increases the unit test pass rate by 6.15% and improves code smell reduction by 3.52%. Generating five refactorings per input further increases the pass rate by 28.8%, suggesting that combining one-shot prompting with multiple refactorings optimizes performance. These findings provide insights into StarCoder2's potential and best practices for integrating LLMs into software refactoring, supporting more efficient and effective code improvement in real-world applications.
Motivation & Objective
- To evaluate the effectiveness of StarCoder2 in improving code quality through refactoring compared to human developers.
- To identify the types of refactoring tasks where LLMs outperform or underperform relative to human expertise.
- To investigate whether prompt engineering techniques—specifically one-shot and chain-of-thought prompting—enhance LLM-generated refactoring quality.
- To assess the impact of generating multiple refactorings per input on code quality and test pass rates.
- To provide actionable insights for integrating LLMs like StarCoder2 into real-world software development workflows for refactoring tasks.
Proposed method
- Conducted an empirical study on 30 open-source Java projects to compare LLM-generated and human-developer refactoring outcomes.
- Used StarCoder2-15B-instruct, a publicly trained LLM, to avoid data leakage and ensure fair comparison with human developers.
- Applied zero-shot, one-shot, and chain-of-thought prompting strategies to evaluate their impact on refactoring quality.
- Generated multiple refactorings per input (up to five) to assess scalability and performance gains from iterative generation.
- Measured code quality improvements using code smell reduction rates, unit test pass rates, and software metrics like cyclomatic complexity.
- Classified refactoring types into systematic (e.g., renaming, extracting methods) and complex (e.g., broken modularization) to compare LLM and human performance across categories.
Experimental results
Research questions
- RQ1How does the code refactoring quality generated by StarCoder2 compare to that of human developers in terms of code smell reduction and test pass rate?
- RQ2Which types of refactoring tasks does StarCoder2 perform more effectively than human developers, and in which areas do developers still outperform the model?
- RQ3To what extent does one-shot prompting improve the quality of LLM-generated refactorings compared to zero-shot prompting?
- RQ4How does generating multiple refactorings per input affect the overall quality and test pass rate of the generated code?
- RQ5Can chain-of-thought prompting significantly enhance the model’s ability to produce high-quality refactorings compared to simpler prompting strategies?
Key findings
- StarCoder2 reduced code smells by 43.36% on average, outperforming human developers, who achieved a 24.27% reduction rate.
- StarCoder2 reduced code smells 20.1% more effectively than developers in automatically generated refactoring tasks.
- One-shot prompting improved the unit test pass rate by 6.15% over zero-shot prompting and reduced code smells at a 3.52% higher rate.
- Generating five refactorings per input increased the unit test pass rate by 28.8% compared to generating only one refactoring per input.
- StarCoder2 excels in systematic refactoring tasks such as Long Statement, Magic Number, Empty Catch Clause, and Long Identifier, where it outperforms developers.
- Human developers outperform StarCoder2 in complex, context-dependent refactoring tasks such as Broken Modularization, Deficient Encapsulation, and Multifaceted Abstraction, due to deeper architectural understanding.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.