用大师指南指导大模型自动重构代码,效果比传统工具更好
Refactoring with LLMs: Bridging Human Expertise and Machine Understanding
- 基于编程大师指南设计指令,引导大模型理解重构目标
- 在真实代码中成功执行61种重构类型,且保持程序语义不变
- 让模型聚焦整体目标比固定步骤更能提升代码质量
代码重构是提升代码质量和可维护性的核心实践,但因耗时耗力且无即时功能收益,开发者常忽视。尽管已有自动化工具,但支持的重构类型有限。本文探索受人类最佳实践启发的指令策略能否增强大语言模型(如GPT-mini和DeepSeek-V3)执行多样化重构任务的能力。基于马丁·福勒的重构指南,我们设计了涵盖动机、步骤与目标的多类指令,覆盖61种常见重构类型。在基准样例和来自GitHub的真实代码片段上评估发现,基于福勒指南的指令使模型能成功完成所有基准重构,并在真实场景中保持程序语义,这是有效重构的关键标准。此外,虽然描述性指令更易理解,但规则化指令在特定场景下表现更优;更重要的是,让模型关注重构总体目标而非固定类型,能进一步显著提升代码质量。
原文摘要 · Abstract (English)
Code refactoring is a fundamental software engineering practice aimed at improving code quality and maintainability. Despite its importance, developers often neglect refactoring due to the significant time, effort, and resources it requires, as well as the lack of immediate functional rewards. Although several automated refactoring tools have been proposed, they remain limited in supporting a broad spectrum of refactoring types. In this study, we explore whether instruction strategies inspired by human best-practice guidelines can enhance the ability of Large Language Models (LLMs) to perform diverse refactoring tasks automatically. Leveraging the instruction-following and code comprehension capabilities of state-of-the-art LLMs (e.g., GPT-mini and DeepSeek-V3), we draw on Martin Fowler's refactoring guidelines to design multiple instruction strategies that encode motivations, procedural steps, and transformation objectives for 61 well-known refactoring types. We evaluate these strategies on benchmark examples and real-world code snippets from GitHub projects. Our results show that instruction designs grounded in Fowler's guidelines enable LLMs to successfully perform all benchmark refactoring types and preserve program semantics in real-world settings, an essential criterion for effective refactoring. Moreover, while descriptive instructions are more interpretable to humans, our results show that rule-based instructions often lead to better performance in specific scenarios. Interestingly, allowing models to focus on the overall goal of refactoring, rather than prescribing a fixed transformation type, can yield even greater improvements in code quality.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。