NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Talking to Itself While Coding: What Makes Comments Help Code Generation?

Dangfeng Pan, Zhensu Sun, Cenyuan Zhang, David Lo, Xiaoning DuarXiv 2026

TL;DR

When Large Language Models (LLMs) generate natural-language comments while coding, they are effectively "talking to themselves." This paper demonstrates that these comments only improve code generation if they contain correct algorithmic content; simply prompting a model to write more comments or change its style provides virtually no benefit. However, prefilling a weaker model's context with correct comments from a stronger model boosts coding accuracy by an average of 17.2%.

What Problem Does This Paper Address?

LLMs frequently generate comments before and during code generation. Intuitively, researchers and engineers have assumed that this "thinking out loud" acts like a Chain-of-Thought preamble, helping the model reason through complex coding tasks.

However, it has remained unclear which properties of comments actually drive performance. Is it the sheer volume of comments? The formatting (inline vs. block)? The intent (explaining why a choice was made vs. what the code does)? Or is it strictly about the correctness of the solution described? Understanding this mechanism is crucial for optimizing prompts, reducing token costs, and designing better code-generation pipelines.

Methods

The authors tackle this question using a mix of observational analyses and controlled interventions:

  1. Observational Analysis: They profile 88 different LLMs to see if natural commenting habits (volume, style, and intent) correlate with code-generation accuracy ($pass@1$). They use an LLM judge (Gemini-3.1-Flash-Lite) to categorize comment intent along the WHY/WHAT axis.
  2. Controlled Interventions (External Prefixing): To isolate comment appearance from actual content, they feed comment blocks written by stronger "source" models (e.g., Claude-4.7-Opus, Grok-4.1-Fast) into weaker "recipient" models (e.g., Qwen3-8B, Gemma-4). They vary the injected comments across several conditions: source-correct, source-wrong, shuffled line order, corrupted (minimally edited to be wrong), and wrong-topic.
  3. Self-Elicitation: They test 10 prompt variants (controlling volume, intent, and format) to see if a model can replicate these performance gains purely through prompt engineering.
Figure 1: The four core sub-experiments. Section 3.1 profiles comment form across 88 models; Section 3.2 profiles comment intent across 13 models. Section 4.1 attaches a stronger source’s comment block to a weaker recipient and varies its content. Section 4.2 asks whether self-prompting can elicit the same lift.

Datasets

The experiments are evaluated across two major benchmarks:

  • LiveCodeBench (v6): A dataset focused on single-file Python competitive programming problems.
  • RepoClassBench: A repository-level benchmark featuring C# and Java tasks that require multi-file awareness and compilation checks.

Key Insights and Ideas

  • Style and Volume are Superficial: There is no natural correlation between how much a model comments and how well it codes. For example, OpenReasoning-Nemotron-32B allocates a mere 0.1% of its output tokens to comments, while Gemini-2.5-Pro uses 71.4%—yet both achieve an identical ~81% $pass@1$ accuracy.
  • Comments Act as Local Hints, Not Strict Derivations: Surprisingly, shuffling the lines of a correct comment block preserves 68% of the performance lift. This indicates that comments do not function as a strict step-by-step logical derivation. Instead, they act as independent local hints that steer the model away from plausible but incorrect strategies.
  • Prompting Can't Force Correctness: Prompt engineering can successfully force a model to change how it comments (e.g., making them denser or forcing a specific style), but it fails to improve coding accuracy because it cannot force a weaker model to magically discover the correct algorithmic solution.

Key Results

  • The Correctness Boost: Prefilling a recipient model with a source-correct comment block increases its $pass@1$ accuracy by an average of 17.2% on LiveCodeBench.
  • The Wrong-Topic Penalty: Supplying comments from an entirely different problem confuses the model, dropping performance by 20.8%. Minimally corrupting just one algorithmic step in a comment also completely eliminates the performance lift.
Recipient pass@1 performance under various source-comment prefix conditions.
  • Self-Elicitation Failure: Prompting models to generate their own detailed comments recovered at most 24% of the performance lift gained from external correct comments.
  • Repository-Level Generalization: The "content effect" successfully generalizes beyond Python to repository-level C# and Java tasks, proving that correct comments consistently improve code generation across languages and architectures.

Limitations and Future Work

The primary limitation identified is the prompting bottleneck: current prompt engineering techniques cannot reliably force an LLM to generate the kind of high-quality, correct algorithmic content that provides a performance lift. If a model doesn't already know how to solve a problem, asking it to write comments first won't save it.

The authors conclude that closing this gap will likely require training-time interventions—such as training models specifically to condition code generation on verified natural language plans—which they leave to future work.

Summary updated

BibTeX
@misc{pan2026talking,
  author = {Dangfeng Pan and Zhensu Sun and Cenyuan Zhang and David Lo and Xiaoning Du},
  title = {{Talking to Itself While Coding: What Makes Comments Help Code Generation?}},
  year = {2026},
  eprint = {2609.09242},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2609.09242},
}
Figure