NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Refactoring Programs Using Large Language Models with Few-Shot Examples

Atsushi Shirafuji, Yusuke Oda, Jun Suzuki, Makoto Morishita, Yutaka Watanobe · DOI

Abstract

Automated code refactoring is a critical task for improving software maintainability and readability, yet it remains challenging for traditional tools to handle complex structural transformations. This paper investigates the efficacy of Large Language Models (LLMs), specifically GPT-3.5, in refactoring Python code. By employing a novel "best-suited" few-shot prompting strategy, the study demonstrates that LLMs can significantly reduce structural complexity while maintaining high functional correctness. Results indicate a 17.35% reduction in cyclomatic complexity and a 95.68% success rate in generating at least one functionally correct refactored program across a dataset of 880 programs.

1. Introduction

In software development, writing code that is merely functional is often insufficient; code must also be readable, maintainable, and efficient. Code refactoring—the process of improving internal structure without altering external behavior—is essential for long-term project health. However, manual refactoring is time-consuming and error-prone, particularly for novice programmers. While existing integrated development environment (IDE) tools can automate simple tasks like variable renaming, they often struggle with complex structural improvements, such as converting nested loops into list comprehensions or optimizing logical branches.

This research explores the potential of LLMs to serve as automated refactoring assistants. The goal is to provide users with simplified versions of their code, creating "teachable moments" that illustrate more elegant logic and idiomatic programming patterns.

2. Methodology

The researchers proposed a methodology centered on Few-Shot Prompting with Example Selection, designed to guide the LLM toward the most relevant refactoring techniques for a given problem.

2.1 Example Preparation and Selection

The study utilized 10 "gold standard" refactoring pairs, representing common Pythonic improvements (e.g., replacing nested if statements with logical operators or utilizing the map() function). To optimize the prompt, the researchers implemented a dynamic selection process:

  1. Evaluation: Each of the 10 examples was tested against the target programming problem using one-shot prompting.
  2. Selection: The top three performing examples for that specific problem type were selected for the final few-shot prompt. This ensured that the LLM was guided by transformations most likely to succeed for the specific logic at hand.

2.2 Validation Framework

To ensure functional correctness, the system utilized an automated judge. Every candidate program generated by the LLM was executed against hidden test cases from the source dataset. A refactoring was only deemed successful if it passed 100% of the functional tests.

3. Dataset

The study leveraged the Aizu Online Judge (AOJ), a platform for competitive programming. The researchers focused on the "Introduction to Programming I" (ITP1) course, which includes 44 introductory problems. From an initial pool of nearly 300,000 Python submissions, the team filtered for unique, correct programs, eventually selecting 20 programs per problem to create a diverse evaluation dataset of 880 programs.

4. Key Results

The quantitative analysis confirms that LLMs are highly effective at structural refactoring:

  • Functional Correctness: The model achieved a 91.11% Pass@1 rate (the first attempt was correct) and a 95.68% Pass@10 rate.
  • Complexity Reduction: The average cyclomatic complexity decreased by 17.35%, indicating a significant simplification of the code's control flow.
  • Conciseness: Programs became more compact, with the average lines of code (LOC) dropping by 25.84% and total character counts decreasing by approximately 20%.
  • Qualitative Improvements: Beyond structural changes, the model improved code "beauty" by standardizing whitespace and replacing cryptic variable names with descriptive identifiers.

5. Discussion and Insights

5.1 The Correctness-Complexity Trade-off

A significant finding was the strong positive correlation (0.79) between functional correctness and cyclomatic complexity. This suggests that as the LLM attempts more aggressive refactoring to reduce complexity, the risk of introducing functional bugs increases.

5.2 Model Behavior and Bias

The study observed that the LLM exhibits a form of "restraint"; it makes fewer changes to programs that are already simple, focusing its efforts on high-complexity code. However, an unexpected behavior was the model's tendency to translate Japanese comments into English or remove them entirely. This highlights an inherent English-centric bias in the underlying training data of GPT-3.5.

6. Limitations and Future Work

While successful, the approach has limitations. The model occasionally refactored code that was already straightforward, sometimes resulting in less readable output. Furthermore, the "all-at-once" refactoring style may be less pedagogically effective than an iterative approach that suggests one small change at a time.

Future research should explore more advanced models (e.g., GPT-4), apply the methodology to different programming languages, and investigate iterative feedback loops to better support the learning process for novice developers.

Summary updated

BibTeX
@inproceedings{shirafuji2023refactoring,
  author = {Atsushi Shirafuji and Yusuke Oda and Jun Suzuki and Makoto Morishita and Yutaka Watanobe},
  title = {{Refactoring Programs Using Large Language Models with Few-Shot Examples}},
  booktitle = {APSEC},
  year = {2023},
  doi = {10.1109/apsec60848.2023.00025},
}
Figure