AceCoder: Utilizing Existing Code to Enhance Code Generation

Jia Li, Yunfei Zhao, Yongmin Li, Ge Li, Zhi Jin · DOI

Abstract

Large Language Models (LLMs) have shown great success in code generation, yet they face unique challenges in requirement understanding and implementation details. We propose AceCoder, a novel prompting technique that mimics human developer workflows through two key mechanisms: guided code generation and example retrieval. Guided code generation prompts LLMs to first analyze requirements and output an intermediate "preliminary" (e.g., test cases) to clarify "what to write." Example retrieval selects diverse, relevant programs to teach the model "how to write." Evaluated on three benchmarks (MBPP, MBJP, MBJSP) across multiple LLMs, AceCoder significantly outperforms state-of-the-art baselines, achieving up to an 88.4% improvement in Pass@1 accuracy.

Introduction

While LLMs like Codex and CodeGeeX are powerful, standard prompting techniques—often borrowed from natural language processing—frequently fail in the domain of software engineering. LLMs often struggle to grasp complex requirements or select the most appropriate APIs, leading to incorrect or suboptimal code.

Human developers rarely write code in a vacuum. Instead, they search for similar snippets on platforms like GitHub and use Test-Driven Development (TDD) to define expected behavior before implementation. AceCoder bridges this gap by formalizing these behaviors into a structured prompting framework designed specifically for code.

Methodology: The AceCoder Framework

AceCoder enhances the standard prompting pipeline by introducing a retrieval-augmented, multi-step generation process.

The AceCoder architecture: from input requirement to final source code via example retrieval and prompt construction.

1. Example Retrieval with Diversity Selection

Rather than providing static or random examples, AceCoder uses a Retriever (BM25) to find code snippets similar to the target requirement. To avoid the "redundancy trap" where the top results are nearly identical, a custom Selector employs an n-gram overlap penalty. This ensures the prompt contains a diverse set of algorithms and API usages, providing broader context for the LLM.

2. Guided Code Generation (The Preliminary)

AceCoder restructures the prompt to include a "Preliminary" step. Instead of mapping a requirement directly to code, the model is prompted to generate intermediate artifacts—most effectively unit test cases. This forces the LLM to define input-output formats and boundary conditions before committing to the final logic. This "thinking" phase significantly reduces errors in the implementation stage.

Experimental Setup

The framework was evaluated using three multi-lingual benchmarks:

  • MBPP (Mostly Basic Python Problems)
  • MBJP (Java version of MBPP)
  • MBJSP (JavaScript version of MBPP)

The technique was applied to three open-source LLMs of varying sizes: CodeGeeX-13B, CodeGen-6B, and InCoder-6B.

Key Results and Analysis

AceCoder demonstrated substantial performance gains across all models and languages. The most significant improvements were observed in the Pass@1 metric, which measures the probability that the first generated snippet is correct.

Performance comparison of AceCoder against standard prompting techniques across different base models and benchmarks.

As shown in the results table, AceCoder consistently outperformed Zero-shot, Chain-of-Thought (CoT), and standard Few-shot prompting. Notable highlights include:

  • Pass@1 Improvements: Improvements reached up to 56.4% for Python, 70.7% for Java, and 88.4% for JavaScript.
  • Model Agnosticism: The technique is effective across different model architectures and sizes (6B to 13B parameters).
  • Human Evaluation: In blind studies, developers preferred AceCoder’s output for its correctness, maintainability, and lack of "code smells."

Discussion: Why Test Cases Matter

A key insight from the study is that for code generation, test cases are superior to natural language reasoning (as used in CoT). Test cases provide unambiguous, structured constraints that align better with the pre-training objectives of code-centric LLMs. Furthermore, the diversity-focused retrieval ensures that the model isn't just seeing more data, but better data, covering a wider range of potential implementation strategies.

Conclusion and Future Work

AceCoder proves that how we prompt is just as critical as model size. By mimicking the human developer's reliance on existing code and test-driven logic, AceCoder sets a new state-of-the-art for LLM-based code generation.

Future research will focus on addressing context window limitations, as the inclusion of multiple examples and preliminaries can be token-intensive. Additionally, the authors aim to explore how retrieving documentation or API signatures can help LLMs utilize entirely new or unseen frameworks.

Summary updated

BibTeX
@misc{li2023acecoder,
  author = {Jia Li and Yunfei Zhao and Yongmin Li and Ge Li and Zhi Jin},
  title = {{AceCoder: Utilizing Existing Code to Enhance Code Generation}},
  year = {2023},
  eprint = {2303.17780},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2303.17780},
}
Figure