Previous Article in Journal
Intelligent Edge Computing and Machine Learning: A Survey of Optimization and Applications
Previous Article in Special Issue
From Divergence to Alignment: Evaluating the Role of Large Language Models in Facilitating Agreement Through Adaptive Strategies
 
 
Font Type:
Arial Georgia Verdana
Font Size:
Aa Aa Aa
Line Spacing:
Column Width:
Background:
This is an early access version, the complete PDF, HTML, and XML versions will be available soon.
Article

Refactoring Loops in the Era of LLMs: A Comprehensive Study

by
Alessandro Midolo
and
Emiliano Tramontana
*
Dipartimento di Matematica e Informatica, University of Catania, 95125 Catania, Italy
*
Author to whom correspondence should be addressed.
Future Internet 2025, 17(9), 418; https://doi.org/10.3390/fi17090418
Submission received: 2 August 2025 / Revised: 9 September 2025 / Accepted: 10 September 2025 / Published: 12 September 2025

Abstract

Java 8 brought functional programming to the Java language and library, enabling more expressive and concise code to replace loops by using streams. Despite such advantages, for-loops remain prevalent in current codebases as the transition to the functional paradigm requires a significant shift in the developer mindset. Traditional approaches for assisting refactoring loops into streams check a set of strict preconditions to ensure correct transformation, hence limiting their applicability. Conversely, generative artificial intelligence (AI), particularly ChatGPT, is a promising tool for automating software engineering tasks, including refactoring. While prior studies examined ChatGPT’s assistance in various development contexts, none have specifically investigated its ability to refactor for-loops into streams. This paper addresses such a gap by evaluating ChatGPT’s effectiveness in transforming loops into streams. We analyzed 2132 loops extracted from four open-source GitHub repositories and classified them according to traditional refactoring templates and preconditions. We then tasked ChatGPT with the refactoring of such loops and evaluated the correctness and quality of the generated code. Our findings revealed that ChatGPT could successfully refactor many more loops than traditional approaches, although it struggled with complex control flows and implicit dependencies. This study provides new insights into the strengths and limitations of ChatGPT in loop-to-stream refactoring and outlines potential improvements for future AI-driven refactoring tools.
Keywords: AI-based code; code generation; software engineering AI-based code; code generation; software engineering

Share and Cite

MDPI and ACS Style

Midolo, A.; Tramontana, E. Refactoring Loops in the Era of LLMs: A Comprehensive Study. Future Internet 2025, 17, 418. https://doi.org/10.3390/fi17090418

AMA Style

Midolo A, Tramontana E. Refactoring Loops in the Era of LLMs: A Comprehensive Study. Future Internet. 2025; 17(9):418. https://doi.org/10.3390/fi17090418

Chicago/Turabian Style

Midolo, Alessandro, and Emiliano Tramontana. 2025. "Refactoring Loops in the Era of LLMs: A Comprehensive Study" Future Internet 17, no. 9: 418. https://doi.org/10.3390/fi17090418

APA Style

Midolo, A., & Tramontana, E. (2025). Refactoring Loops in the Era of LLMs: A Comprehensive Study. Future Internet, 17(9), 418. https://doi.org/10.3390/fi17090418

Note that from the first issue of 2016, this journal uses article numbers instead of page numbers. See further details here.

Article Metrics

Back to TopTop