PulseExploreJournal ClubDebatesTrendingResearchersJournals
Instagram
HomeExploreJournal ClubTrending
Synapse
⌘+K
Synapse
September 17, 2025Future Internet6 citationsOpen Access

Refactoring Loops in the Era of LLMs: A Comprehensive Study

View Full Paper
AMAlessandro MidoloETEmiliano Tramontana

Key Points

  • ChatGPT effectively refactored a higher number of loops compared to traditional methods, enhancing code efficiency and clarity.
  • The analysis included 2132 loops from four open-source GitHub repositories, highlighting significant results in refactoring success.
  • Refactoring was based on predefined templates, which ChatGPT navigated well, although it faced challenges with complex control flows.
  • This study reveals both the efficacy and limitations of using AI-driven tools like ChatGPT for software engineering tasks.

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.

Ask AI
Helpful
Bookmark
Share
View Full Paper

Cite This Study

Midolo et al. (2025) studied this question.

synapsesocial.com/papers/68d45e6a31b076d99fa5f0eahttps://doi.org/10.3390/fi17090418
Ask AI
Helpful
Bookmark
Share
View Full Paper

Also Consider

Synapse has enriched 5 closely related papers on similar clinical questions. Consider them for comparative context:

  1. 1Automated Refactoring of Non-Idiomatic Python Code: A Differentiated Replication with LLMS2025 · 2 citations
  2. 2Safe stream-based programming with refinement types2018 · 6 citations
  3. 3ChatGPT Prompt Patterns for Improving Code Quality, Refactoring, Requirements Elicitation, and Software Design2024 · 263 citations
  4. 4Crossing the gap from imperative to functional programming through refactoring2013 · 59 citations
  5. 5How We Refactor, and How We Know It2011 · 548 citations