Table of Contents
Fetching ...

From Restructuring to Stabilization: A Large-Scale Experiment on Iterative Code Readability Refactoring with Large Language Models

Norman Peitek, Julia Hess, Sven Apel

TL;DR

This article systematically study the capabilities of LLMs for code refactoring with a specific focus on improving code readability, revealing three main insights: first, iterative code refactoring exhibits an initial phase of restructuring followed by stabilization, which suggests that LLMs possess an internalized understanding of an"optimally readable"version of code.

Abstract

Large language models (LLMs) are increasingly used for automated code refactoring tasks. Although these models can quickly refactor code, the quality may exhibit inconsistencies and unpredictable behavior. In this article, we systematically study the capabilities of LLMs for code refactoring with a specific focus on improving code readability. We conducted a large-scale experiment using GPT5.1 with 230 Java snippets, each systematically varied and refactored regarding code readability across five iterations under three different prompting strategies. We categorized fine-grained code changes during the refactoring into implementation, syntactic, and comment-level transformations. Subsequently, we investigated the functional correctness and tested the robustness of the results with novel snippets. Our results reveal three main insights: First, iterative code refactoring exhibits an initial phase of restructuring followed by stabilization. This convergence tendency suggests that LLMs possess an internalized understanding of an "optimally readable" version of code. Second, convergence patterns are fairly robust across different code variants. Third, explicit prompting toward specific readability factors slightly influences the refactoring dynamics. These insights provide an empirical foundation for assessing the reliability of LLM-assisted code refactoring, which opens pathways for future research, including comparative analyses across models and a systematic evaluation of additional software quality dimensions in LLM-refactored code.

From Restructuring to Stabilization: A Large-Scale Experiment on Iterative Code Readability Refactoring with Large Language Models

TL;DR

This article systematically study the capabilities of LLMs for code refactoring with a specific focus on improving code readability, revealing three main insights: first, iterative code refactoring exhibits an initial phase of restructuring followed by stabilization, which suggests that LLMs possess an internalized understanding of an"optimally readable"version of code.

Abstract

Large language models (LLMs) are increasingly used for automated code refactoring tasks. Although these models can quickly refactor code, the quality may exhibit inconsistencies and unpredictable behavior. In this article, we systematically study the capabilities of LLMs for code refactoring with a specific focus on improving code readability. We conducted a large-scale experiment using GPT5.1 with 230 Java snippets, each systematically varied and refactored regarding code readability across five iterations under three different prompting strategies. We categorized fine-grained code changes during the refactoring into implementation, syntactic, and comment-level transformations. Subsequently, we investigated the functional correctness and tested the robustness of the results with novel snippets. Our results reveal three main insights: First, iterative code refactoring exhibits an initial phase of restructuring followed by stabilization. This convergence tendency suggests that LLMs possess an internalized understanding of an "optimally readable" version of code. Second, convergence patterns are fairly robust across different code variants. Third, explicit prompting toward specific readability factors slightly influences the refactoring dynamics. These insights provide an empirical foundation for assessing the reliability of LLM-assisted code refactoring, which opens pathways for future research, including comparative analyses across models and a systematic evaluation of additional software quality dimensions in LLM-refactored code.
Paper Structure (79 sections, 1 equation, 27 figures, 6 tables)

This paper contains 79 sections, 1 equation, 27 figures, 6 tables.

Figures (27)

  • Figure 1: Overview of the experiment flow from source data to data analysis.
  • Figure 2: Overview of the three types of comparison (non-consecutive comparisons are omitted for readability)
  • Figure 3: Schematic visualization of similarity score evolution in back-and-forth changes with v0 $=$ v2 $\neq$ v1
  • Figure 4: Average absolute metrics across refactorings ( Original, PromptGeneral)). The shaded areas indicate standard deviation.
  • Figure 5: Detailed proportional distribution of code changes across iterations ( Original, PromptGeneral).
  • ...and 22 more figures