用大模型分析开源代码重构动机,发现多数为提升可读性和维护性。
What Were You Thinking? An LLM-Driven Large-Scale Study of Refactoring Motivations in Open-Source Projects
- 用大模型从版本记录中自动识别重构背后的真正原因。
- 80%判断准确,但仅47%与已有研究一致,22%补充了更详细理由。
- 适合想系统化推进重构的开发团队和研究者参考。
代码重构能提升软件质量而不改变外部行为,但其成本高、耗时长,限制了广泛应用。本文通过大规模实证研究,利用大语言模型(LLMs)从版本控制数据中分析开发者重构行为,识别其深层动机,并与文献中已有动机对比。结果显示,LLMs在80%案例中与人工判断一致,但仅47%与文献动机吻合;模型能为22%的动机补充更细致的理由,常强调代码可读性、清晰度和结构优化。多数重构动机偏向实用,关注简化和可维护性。尽管与开发者体验和代码可读性相关的指标得分最高,但它们与动机类别的相关性较弱。结论是:大模型虽能有效捕捉表面动机,但在架构层面推理能力有限;其价值在于提供局部解释,结合软件度量可形成混合方法,有望更系统地优先安排重构,平衡短期改进与长期架构目标。
原文摘要 · Abstract (English)
Context. Code refactoring improves software quality without changing external behavior. Despite its advantages, its benefits are hindered by the considerable cost of time, resources, and continuous effort it demands. Aim. Understanding why developers refactor, and which metrics capture these motivations, may support wider and more effective use of refactoring in practice. Method. We performed a large-scale empirical study to analyze developers refactoring activity, leveraging Large Language Models (LLMs) to identify underlying motivations from version control data, comparing our findings with previous motivations reported in the literature. Results. LLMs matched human judgment in 80% of cases, but aligned with literature-based motivations in only 47%. They enriched 22% of motivations with more detailed rationale, often highlighting readability, clarity, and structural improvements. Most motivations were pragmatic, focused on simplification and maintainability. While metrics related to developer experience and code readability ranked highest, their correlation with motivation categories was weak. Conclusions. We conclude that LLMs effectively capture surface-level motivations but struggle with architectural reasoning. Their value lies in providing localized explanations, which, when combined with software metrics, can form hybrid approaches. Such integration offers a promising path toward prioritizing refactoring more systematically and balancing short-term improvements with long-term architectural goals.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。