Introduction
Large Language Models (LLMs) have revolutionized automated programming, yet they frequently struggle with complex logical reasoning and the strict syntactical requirements of low-level languages. While techniques like Chain-of-Thought (CoT) have proven effective in mathematical domains by allowing models to "think out loud," their application to software engineering remains an active area of research. This paper investigates whether forcing a model to generate an intermediate representation—such as a natural language sketch, pseudo-code, or a different programming language—improves the quality of the final code in a target language.
Methodology
The researchers proposed a two-stage prompting strategy to evaluate the impact of intermediate steps:
- Stage 1 (Drafting): The model receives a task description and is tasked with generating a solution in an intermediate language (e.g., C++, Go, Java, Python, Rust, Pseudo-code, or Natural Language).
- Stage 2 (Finalization): The model uses the output from Stage 1 as context to generate the final code in the desired target language.
To isolate the variables driving performance, the study included two critical control experiments. First, a Ground Truth experiment provided the model with perfect, human-written intermediate solutions to determine if the final code's success depended on the accuracy of the draft. Second, a Repetitive Prompting experiment asked the model to generate code twice without intermediate steps, testing whether improvements were simply a result of increased "thinking time" or context length.
Experimental Setup
The study utilized HumanEval-X, a multilingual benchmark featuring 164 programming problems across five languages: Python, C++, Java, Go, and Rust. The researchers evaluated a diverse suite of 11 models, ranging from small-scale architectures like Phi-3-mini and Deepseek-coder-1.3b to industry leaders like GPT-4 and the CodeLlama family (up to 70B).
Key Insights
The Dominance of Natural Language
One of the most significant findings is that natural language sketches consistently outperformed formal programming languages as an intermediate step. The flexibility of natural language appears to allow models to map out logical structures without being constrained by the rigid syntax of a formal language. This suggests that for code generation, a conceptual "blueprint" is more valuable than a preliminary draft in a different syntax.
The Chain-of-Thought Effect
Interestingly, the researchers found only a weak correlation between the correctness of the intermediate solution and the correctness of the final code. A model could produce a buggy intermediate draft in C++ yet still generate a perfect final solution in Rust. This indicates that the benefit of the intermediate step is primarily a "Chain-of-Thought" effect—the act of processing the logic within the context window—rather than a literal translation process.
Scaling and Model Size
Model capacity plays a decisive role in the effectiveness of this technique. Models with more than 10 billion parameters generally saw significant performance boosts. In contrast, smaller models (e.g., Phi-3 or Deepseek-1.3b) often experienced a performance degradation. The researchers hypothesize that the additional context of an intermediate step may overwhelm the limited reasoning and attention capabilities of smaller architectures.
The Power of Repetition
For the GPT family, simply prompting the model to attempt the problem a second time (using the first attempt as context) yielded substantial gains. GPT-4, for instance, showed a 20.1% improvement in Rust generation through simple repetition, suggesting that these models possess a latent ability to self-correct when given the opportunity to iterate.
Results and Performance
- Average Gains: For larger models, natural language intermediate steps led to the highest average pass rates (e.g., 59.2% for Python compared to 58.6% for direct generation).
- Language Difficulty: Rust, often considered a "harder" language due to its strict borrow checker and smaller presence in training datasets, benefited the most from intermediate reasoning steps.
- GPT-4 Excellence: GPT-4 reached its peak performance using natural language or pseudo-code, achieving improvements of approximately 6% in Java and Python.
Limitations and Conclusion
While intermediate languages offer a clear path to better code, the study notes several challenges. There is no "universal" formal programming language that helps across all models; what aids one model may hinder another. Furthermore, two-stage and repetitive prompting significantly increase inference costs by doubling or tripling token usage.
In conclusion, while natural language serves as a powerful intermediate bridge for complex coding tasks, the strategy must be balanced against the increased latency and cost, particularly for smaller models that struggle to process the additional logical overhead.
Summary updated