Skip to main content
QUICK REVIEW

[论文解读] Revisiting the Role of Natural Language Code Comments in Code Translation

Monika Gupta, Ajay Kumar Meena|arXiv (Cornell University)|Jan 23, 2026
Natural Language Processing TechniquesComputer Science被引用 0
一句话总结

该论文在五种语言中实证研究自然语言代码注释如何影响基于大语言模型的代码翻译,并提出 COMMENTRA 用于有选择地添加注释以改进翻译。

ABSTRACT

The advent of large language models (LLMs) has ushered in a new era in automated code translation across programming languages. Since most code-specific LLMs are pretrained on well-commented code from large repositories like GitHub, it is reasonable to hypothesize that natural language code comments could aid in improving translation quality. Despite their potential relevance, comments are largely absent from existing code translation benchmarks, rendering their impact on translation quality inadequately characterised. In this paper, we present a large-scale empirical study evaluating the impact of comments on translation performance. Our analysis involves more than $80,000$ translations, with and without comments, of $1100+$ code samples from two distinct benchmarks covering pairwise translations between five different programming languages: C, C++, Go, Java, and Python. Our results provide strong evidence that code comments, particularly those that describe the overall purpose of the code rather than line-by-line functionality, significantly enhance translation accuracy. Based on these findings, we propose COMMENTRA, a code translation approach, and demonstrate that it can potentially double the performance of LLM-based code translation. To the best of our knowledge, our study is the first in terms of its comprehensiveness, scale, and language coverage on how to improve code translation accuracy using code comments.

研究动机与目标

  • 评估自然语言代码注释如何影响基于大语言模型的代码翻译质量。
  • 分析注释特征(意图、密度、语言、位置)及其对翻译性能的影响。
  • 开发并评估基于注释的翻译框架 COMMENTRA,能有选择地插入注释以改善结果。
  • 提供跨语言基准和洞察,以指导翻译管道中的注释使用。

提出的方法

  • 从 AVATAR 和 CodeNet 中收集超过 1100 个独特代码示例,涵盖 C、C++、Go、Java、Python。
  • 使用多种注释生成的 LLM 生成注释,并利用若干翻译 LLM 对带注释和不带注释的代码进行翻译。
  • 系统性地改变注释因素(意图、密度、语言、放置位置),并通过编译和测试结果来衡量翻译成功。
  • 引入 COMMENTRA,一种渐进式翻译方法,只有在初始翻译失败时才添加注释,以提高效率和准确性。
Figure 1 : Experimental Setup; The exact prompts used are also shown here.
Figure 1 : Experimental Setup; The exact prompts used are also shown here.

实验结果

研究问题

  • RQ1RQ1 - 代码注释的有用性:自然语言代码注释是否有助于提升 LLM 的翻译性能?
  • RQ2RQ2 - 代码注释的意图:我们能否对注释意图进行分类并理解在不同意图下的效用?
  • RQ3RQ3 - 注释密度、代码注释语言:注释密度和语言如何影响翻译准确性?
  • RQ4RQ4 - 注释位置:注释的放置如何影响翻译结果?

主要发现

  • 代码注释可能有助也可能损害翻译性能,提升与下降取决于模型和语言对。
  • 像 GPT 和 DeepSeek 这样的注释模型通常比某些替代模型带来更好的改进,但效果因场景而异。
  • 就 Java-到-Python 和 Python-到-Java 的翻译而言,英文注释通常提供最强的收益,但存在一些例外。
  • 对注释密度的任意限制并不始终提升性能;对有选择地注释的有效指导仍待完善。
  • 在代码中注释通常优于伪代码或独立的方法规范,能改善翻译质量。
  • 所提出的 COMMENTRA 框架通过仅在初始翻译失败时插入注释,带来显著的改进。
Figure 2 : Venn diagrams depicting increase and decrease in LLMs performance in the commented code samples. Left and center diagrams show the overlap between uncommented successful and successfully translated model-commented samples; the right diagrams show the overlap between the various successful
Figure 2 : Venn diagrams depicting increase and decrease in LLMs performance in the commented code samples. Left and center diagrams show the overlap between uncommented successful and successfully translated model-commented samples; the right diagrams show the overlap between the various successful

阅读本论文

在 Nubint 阅读器中打开原文,边读边就任意段落提问。

在 Nubint 中阅读本论文

本解读由 AI 根据论文原文生成,引用前请核对原文。