Automated Extract Method Refactoring with Open-Source LLMs: A Comparative Study
Authors:
- 19 November 2025
- conference paper
- Published by Institute of Electrical and Electronics Engineers (IEEE)
- p. 113-122
Abstract
Automating the Extract Method refactoring (EMR) remains challenging and largely manual despite its importance in improving code readability and maintainability. Recent advances in open-source, resource-efficient Large Language Models (LLMs) offer promising new approaches for automating such high-level tasks. In this work, we critically evaluate five state-of-the-art open-source LLMs, spanning 3B to 8B parameter sizes, on the EMR task for Python code. We systematically assess functional correctness and code quality using automated metrics and investigate the impact of prompting strategies by comparing one-shot prompting to a Recursive criticism and improvement (RCI) approach. RCI-based prompting consistently outperforms one-shot prompting in test pass rates and refactoring quality. The best-performing models, Deepseek-Coder-RCI and Qwen2.5-Coder-RCI, achieve test pass percentage (TPP) scores of 0.829 and 0.808, while reducing lines of code (LOC) per method from 12.103 to 6.192 and 5.577, and cyclomatic complexity (CC) from 4.602 to 3.453 and 3.294, respectively. A developer survey on RCI-generated refactorings shows over $\mathbf{7 0 \%}$ acceptance, with Qwen2.5-Coder rated highest across all evaluation criteria. In contrast, the original code scored below neutral, particularly in readability and maintainability, underscoring the benefits of automated refactoring guided by quality prompts. While traditional metrics like CC and LOC provide useful signals, they often diverge from human judgments, emphasizing the need for human-in-the-loop evaluation. Our open-source benchmark offers a foundation for future research on automated refactoring with LLMs.
Keywords
All Related Versions
This publication has 20 references indexed in Scilit:
- Design pattern recognition: a study of large language modelsEmpirical Software Engineering, 2025
- Iterative Refactoring of Real-World Open-Source Programs with Large Language Models2024
- A Comparative Study of Automated Refactoring ToolsIEEE Access, 2024
- Just-in-time code duplicates extractionInformation and Software Technology, 2023
- An automated extract method refactoring approach to correct the long method code smellJournal of Systems and Software, 2022
- Code smells and refactoring: A tertiary systematic review of challenges and observationsJournal of Systems and Software, 2020
- Characteristics of method extractions in Java: a large scale empirical studyEmpirical Software Engineering, 2020
- Automatic software refactoring: a systematic literature reviewSoftware Quality Journal, 2019
- Model refactoring by example: A multi‐objective search based software engineering approachJournal of Software: Evolution and Process, 2017
- MORE: A multi‐objective refactoring recommendation approach to introducing design patterns and fixing code smellsJournal of Software: Evolution and Process, 2017