arXiv:2512.01141cs.SEcs.LG2025-12

让代码变量名自动变清晰,提升可读性和开发效率。

Neural Variable Name Repair: Learning to Rename Identifiers for Readability

  • 用LLM学习修复变量名,通过遮蔽原名并生成描述性替代名。
  • 最佳模型达43.1%完全匹配率,82.03分部分匹配分。
  • 适合需要提升代码可读性的开发者和自动化工具构建者。

开发者常面对变量名模糊或误导的源码,且因团队快速迭代,大量函数缺乏文档,这阻碍理解、增加隐性错误风险,并使人类与大语言模型更难推理代码。本文研究变量名修复:给定一个真实C++函数中某局部变量或参数名被替换为占位符(如ID 1),目标是生成自然且描述性强的替代名。我们基于BigCode的The Stack C++部分,利用Tree-sitter解析函数,遮蔽单一标识符,并将原始名称作为监督信号,自动构建该任务。在Llama 3.1-8B基础上,构建包含(i)稳定微调的预热与丢弃策略,(ii)高效适配器的LoRA模块,以及(iii)基于双编码器的重排序器筛选前k个生成候选。评估使用精确匹配、Top-5命中率及嵌入式部分相似度分数(0-100),其中后者对近义词与格式变体(如jsonValue vs. json)给予部分评分。在200个保留函数测试集上,零样本基线仅达6.1%精确匹配;最优LoRA微调模型(含预热与丢弃)实现43.1%精确匹配、50.2% Top-5命中率、82.03分部分匹配分。双编码器重排序器进一步提升选择质量,且不修改生成器本身,表明任务特化微调加重排序是实用变量名修复工具的有前景路径。

原文摘要 · Abstract (English)

Developers routinely work with source files whose variable names are generic or misleading, and with teams moving quickly, many functions are left undocumented. This slows comprehension, increases the risk of subtle bugs, and makes it harder for both humans and large language models (LLMs) to reason about code. We study variable name repair: given a real C++ function where all occurrences of one local or parameter name have been replaced by a placeholder (e.g. ID 1), the goal is to generate a natural, descriptive replacement name. We automatically construct this task from the C++ portion of BigCode's The Stack by parsing functions with Tree-sitter, masking a single identifier, and treating the original name as supervision. On top of Llama 3.1-8B, we build a pipeline with (i) warmup and dropout schedules for more stable fine-tuning, (ii) LoRA adapters for efficient specialization on identifier repair, and (iii) a dual-encoder reranker over top-k generator candidates. We evaluate using exact match, Top-5 Hit, and an embedding-based partial similarity score (0-100) that gives credit for near synonyms and format variants (e.g., jsonValue vs. json). On a held-out set of 200 C++ functions, a zero-shot Llama 3.1 baseline reaches 6.1 percent exact match. Our best LoRA-tuned model (with warmup and dropout) achieves 43.1 percent exact match, 50.2 percent Top-5 Hit, and an 82.03 partial-match score. A dual encoder reranker further improves selection quality without modifying the underlying generator, suggesting that task-specific fine-tuning plus reranking is a promising approach for practical identifier repair tools.

代码修复变量命名LLM应用可读性

Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。