IDEAS home Printed from https://ideas.repec.org/a/gam/jftint/v17y2025i9p418-d1748323.html
   My bibliography  Save this article

Refactoring Loops in the Era of LLMs: A Comprehensive Study

Author

Listed:
  • Alessandro Midolo

    (Dipartimento di Matematica e Informatica, University of Catania, 95125 Catania, Italy)

  • Emiliano Tramontana

    (Dipartimento di Matematica e Informatica, University of Catania, 95125 Catania, Italy)

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.

Suggested Citation

  • Alessandro Midolo & Emiliano Tramontana, 2025. "Refactoring Loops in the Era of LLMs: A Comprehensive Study," Future Internet, MDPI, vol. 17(9), pages 1-27, September.
  • Handle: RePEc:gam:jftint:v:17:y:2025:i:9:p:418-:d:1748323
    as

    Download full text from publisher

    File URL: https://www.mdpi.com/1999-5903/17/9/418/pdf
    Download Restriction: no

    File URL: https://www.mdpi.com/1999-5903/17/9/418/
    Download Restriction: no
    ---><---

    More about this item

    Keywords

    ;
    ;
    ;

    Statistics

    Access and download statistics

    Corrections

    All material on this site has been provided by the respective publishers and authors. You can help correct errors and omissions. When requesting a correction, please mention this item's handle: RePEc:gam:jftint:v:17:y:2025:i:9:p:418-:d:1748323. See general information about how to correct material in RePEc.

    If you have authored this item and are not yet registered with RePEc, we encourage you to do it here. This allows to link your profile to this item. It also allows you to accept potential citations to this item that we are uncertain about.

    We have no bibliographic references for this item. You can help adding them by using this form .

    If you know of missing items citing this one, you can help us creating those links by adding the relevant references in the same way as above, for each refering item. If you are a registered author of this item, you may also want to check the "citations" tab in your RePEc Author Service profile, as there may be some citations waiting for confirmation.

    For technical questions regarding this item, or to correct its authors, title, abstract, bibliographic or download information, contact: MDPI Indexing Manager (email available below). General contact details of provider: https://www.mdpi.com .

    Please note that corrections may take a couple of weeks to filter through the various RePEc services.

    IDEAS is a RePEc service. RePEc uses bibliographic data supplied by the respective publishers.