Automating Best-Practice Refactoring in Java via Multi-Agent Planning and Verification
Discuss this preprint
Start a discussion What are Sciety discussions?Listed in
This article is not in any list yet, why not save it to one of your lists.Abstract
Best-practice rules capture widely accepted coding conventions thatimprove software maintainability, safety, and extensibility. Modern static analysistools, such as PMD and SonarQube, can accurately detect best-practiceviolations, yet repairing these issues remains largely manual, time-consuming,and error-prone. Although large language models show promise in code generation,directly applying them to refactoring often yields unsafe or incompletetransformations, especially for semantically subtle best-practice rules. In this paper, we propose BestRefactor, the first automated approachthat explicitly targets Java best-practice violations at scale. BestRefactoradopts a multi-agent, recipe-guided framework that decomposes refactoringinto three coordinated stages: (i) a Planning Agent that determines applicabilityand safety, (ii) a Refactoring Agent that applies rule-specific transformationrecipes, and (iii) a Verification Agent that validates correctness and rulecompliance. This design enables reliable, behavior-preserving refactoring beyondna¨ıve single-step LLM rewriting. We implement BestRefactor as a practicaltool integrated with PMD and evaluate it on 10 real-world Java librariesspanning five application domains, comprising 844 detected best-practice violations.Experimental results show that BestRefactor successfully producesverified repairs for 72.6% of all detected violations end to end, outperforminga direct LLM baseline by over 11 percentage points in verified repair rate. Anablation study confirms that planning, recipe guidance, and verification eachplay a critical role in achieving reliable refactoring. Finally, our performanceanalysis demonstrates that BestRefactor is practical in terms of runtime andcost.