Comments as Natural Logic Pivots: Improve Code Generation via Comment Perspective

Yijie Chen, Yijin Liu, Fandong Meng, Yufeng Chen, Jinan Xu, Jie Zhou

Abstract

Large Language Models (LLMs) have demonstrated remarkable capabilities in code generation, often leveraging Chain-of-Thought (CoT) prompting to decompose complex problems. However, smaller models (3B–7B parameters) frequently struggle with the transition from high-level natural language reasoning to formal code syntax, leading to performance degradation. We present MANGO (comMents As Natural loGic pivOts), a framework that utilizes inline code comments as a bridge between requirements and executable code. By combining Comment Contrastive Learning (CCL) and Logical Comment Prompting (LCP), MANGO significantly enhances the accuracy and robustness of small-to-medium sized code LLMs.

Introduction

While state-of-the-art models like GPT-4 excel at multi-step reasoning, smaller open-source models often encounter a "logic gap" when generating code. Standard CoT strategies require the model to generate a separate block of natural language reasoning before writing code. For smaller models, this separation often results in a disconnect; a mistake in the initial reasoning block propagates into the code, or the model fails to translate its correct plan into valid syntax.

MANGO addresses this by treating comments not just as documentation, but as logic pivots—intermediate reasoning steps interleaved directly within the code. This approach ensures that the model's "thoughts" are locally anchored to the specific lines of code they describe, reducing the cognitive load and the likelihood of logic drift.

The MANGO framework treats comments as essential logic pivots that interleave reasoning with code execution.

Methodology

The MANGO framework consists of two core strategies designed to align natural language logic with code structure:

  1. Comment Contrastive Learning (CCL): During fine-tuning, the model is trained to distinguish between well-commented code and code without comments. By using a contrastive loss function, the model learns to assign higher probability to code snippets where comments provide structural guidance, effectively internalizing the value of comments as a roadmap for generation.
  2. Logical Comment Prompting (LCP): Instead of the generic "think step-by-step" instruction, LCP prompts the model to generate code "with comments to explain the logic." This forces the model to perform problem decomposition inline. Because the comments are part of the code block, the model maintains a tighter alignment between its reasoning and the resulting implementation.

Experimental Results

We evaluated MANGO across several popular model architectures, including StarCoder and WizardCoder, using the HumanEval and MBPP benchmarks. The results demonstrate that MANGO consistently outperforms both standard Supervised Fine-Tuning (SFT) and traditional Chain-of-Thought (CoT) approaches.

Performance comparison of MANGO against SFT, CoT, and LCP baselines on HumanEval and MBPP benchmarks.

As shown in the results table, MANGO provides substantial gains. For instance, on StarCoder-7B, MANGO improved the Pass@10 score on HumanEval by approximately 7.5 percentage points over the SFT baseline. Notably, while standard CoT often caused a performance decline in 3B models (e.g., WizardCoder-3B on HumanEval), MANGO's LCP strategy consistently yielded improvements, proving that "thinking inline" is a more effective strategy for smaller models than "thinking before acting."

Key Insights

  • Lower Cognitive Barrier: Analysis of token-level cross-entropy loss reveals that models find it significantly easier to generate natural language when it is formatted as a code comment rather than a separate CoT block. This suggests that the code context provides a helpful constraint for the model's reasoning.
  • Robustness to Prompting: Small models are typically sensitive to prompt variations. However, LCP proved to be highly stable, providing consistent performance gains regardless of the specific wording used to request comments, whereas standard CoT performance fluctuated significantly.
  • Error Mitigation: Detailed error analysis indicates that MANGO specifically reduces "All Wrong Answer" (AWA) errors, suggesting that the comment-based decomposition helps the model better grasp the fundamental requirements of the problem description.

Conclusion

MANGO demonstrates that for small-to-medium sized LLMs, the path to better code generation lies in tighter integration between reasoning and implementation. By leveraging comments as natural logic pivots, we can bridge the gap between human requirements and machine-executable code, making smaller models more capable and reliable for real-world programming tasks.

Summary updated

BibTeX
@inproceedings{chen2024comments,
  author = {Yijie Chen and Yijin Liu and Fandong Meng and Yufeng Chen and Jinan Xu and Jie Zhou},
  title = {{Comments as Natural Logic Pivots: Improve Code Generation via Comment Perspective}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2024},
  doi = {10.18653/v1/2024.findings-acl.420},
}
Figure