Automated Extract Method Refactoring with Open-Source LLMs: A Comparative Study

Sivajeet Chand, Melih Kilic, Roland Würsching, Sushant Kumar Pandey, Alexander PretschnerarXiv 2025 · DOI

Abstract

This study benchmarks five small, open-source Large Language Models (LLMs) with 3B to 8B parameters on their capability to perform "Extract Method" refactoring (EMR) in Python. By implementing a Recursive Criticism and Improvement (RCI) prompting strategy, top-performing models such as Qwen2.5-Coder and DeepSeek-Coder achieved over 80% functional correctness and 70% developer acceptance. These findings suggest that resource-efficient, locally deployable models are now viable for complex, automated software engineering tasks.

Introduction

Code refactoring—the process of improving internal code structure without altering external behavior—is fundamental to long-term software maintainability. Among various refactoring types, "Extract Method" refactoring (EMR) is the most prevalent, involving the migration of a code fragment into a new, descriptively named function.

Despite its importance, EMR is often a manual and error-prone process. Traditional rule-based tools frequently lack the semantic understanding required for complex codebases. While large-scale proprietary models like GPT-4 have shown potential, many organizations require open-source, privacy-compliant solutions that can operate on standard hardware. This research investigates the efficacy of small-scale open-source LLMs and the impact of iterative prompting strategies on refactoring quality.

Methodology

The researchers developed an end-to-end evaluation pipeline to assess model performance across several dimensions, comparing standard one-shot prompting against a recursive feedback loop.

Figure 1: Comparison of the One-shot and RCI prompting workflows for automated refactoring.

Model Selection and Prompting

Five state-of-the-art open-source models were evaluated: Qwen1.5-7B, DeepSeek-Coder-6.7B, Llama-3.2-3B, Qwen2.5-Coder-7B, and Phi-4-mini-3.8B. The study compared two primary prompting strategies:

  1. One-Shot Prompting: Providing the model with a single example of a successful refactoring.
  2. Recursive Criticism and Improvement (RCI): An iterative loop where, if the initial output fails functional tests, the error message is fed back to the model for up to two additional self-correction attempts.

Evaluation Framework and Dataset

Performance was measured using Test Pass Percentage (TPP), average Lines of Code (LOC) per method, and Cyclomatic Complexity (CC). Additionally, a developer survey involving nine professional engineers was conducted to rate qualitative aspects like readability and maintainability.

The study utilized a subset of CodeNet, selecting 840 Python submissions across 21 problems. Samples were filtered for a Cyclomatic Complexity ≥ 3 to ensure they warranted refactoring, and all baseline samples were verified against test cases to ensure functional correctness before the experiment.

Results and Discussion

The experimental results highlight the significant impact of iterative prompting and the evolving capabilities of small-scale models.

The Impact of Iteration

The RCI strategy consistently outperformed one-shot prompting across all models. For the leading models, the iterative feedback loop resulted in a performance increase of over 10 percentage points in functional correctness. Notably, most successful corrections occurred during the first retry.

Figure 4: Test Pass Percentage (TPP) across different models and prompting strategies, showing the superiority of the RCI approach.

Quantitative Performance and Human Preference

DeepSeek-Coder-RCI (82.9% TPP) and Qwen2.5-Coder-RCI (80.8% TPP) emerged as the top performers in functional correctness. These models effectively reduced the average LOC per method from 12.1 to approximately 5.6 and lowered cyclomatic complexity from 4.6 to roughly 3.3.

A key finding was the discrepancy between automated metrics and human preference. While DeepSeek-Coder achieved the highest TPP, human developers preferred the output of Qwen2.5-Coder, citing better readability. This suggests that traditional metrics like LOC and CC can be misleading if used in isolation; for instance, Llama-3.2 produced low complexity scores by generating incomplete code that failed functional tests.

Conclusion

This study demonstrates that 7B-class open-source models are capable of producing high-quality refactorings that professional developers find acceptable. Future research should expand this benchmark to statically typed languages and a broader range of refactoring types beyond "Extract Method."

Summary updated

BibTeX
@misc{chand2025automated,
  author = {Sivajeet Chand and Melih Kilic and Roland Würsching and Sushant Kumar Pandey and Alexander Pretschner},
  title = {{Automated Extract Method Refactoring with Open-Source LLMs: A Comparative Study}},
  year = {2025},
  eprint = {2510.26480},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2510.26480},
}
Figure