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

SkCoder: A Sketch-based Approach for Automatic Code Generation

Jia Li, Yongmin Li, Ge Li, Zhi Jin, Yiyang Hao, Xing Hu · DOI

Abstract

Recently, deep learning techniques have shown great success in automatic code generation. Inspired by code reuse, some researchers propose copy-based approaches that can copy content from similar code snippets to obtain better performance. Practically, human developers recognize the content in similar code that is relevant to their needs, which can be viewed as a code sketch. The sketch is further edited to the desired code. However, existing copy-based approaches ignore code sketches and tend to repeat similar code without necessary modifications, leading to incorrect results. In this paper, we propose SkCoder, a sketch-based code generation approach to mimic developers' code reuse behavior. Given a natural language requirement, SkCoder retrieves a similar code snippet, extracts relevant parts as a code sketch, and edits the sketch into the desired code. Our motivations are that the extracted sketch provides a well-formed pattern for telling models “how to write,” while post-editing adds requirement-specific details. We conduct experiments on two public datasets and a new dataset, AixBench-L. Experimental results show that SkCoder outperforms the state-of-the-art CodeT5-base by up to 35.39% and improves multiple code generation models by up to 120.1% in Pass@1.

Introduction

Automatic code generation aims to translate natural language (NL) descriptions into executable code. While Large Language Models (LLMs) have made significant strides, they often struggle with complex logic or specific API usage. To address this, "copy-based" models were introduced to leverage existing code snippets. However, these models often suffer from "over-copying," where irrelevant details like specific variable names or unrelated logic from the retrieved snippet are incorrectly integrated into the output.

The human-inspired Retrieve-Sketch-Edit workflow.

We observe that human developers do not simply copy-paste; they identify structural patterns—a "sketch"—and then adapt it. SkCoder formalizes this intuition into a three-stage pipeline: Retrieve, Sketch, and Edit.

Methodology

The SkCoder framework consists of three primary components designed to separate structural patterns from specific implementation details.

The SkCoder system architecture comprising Retriever, Sketcher, and Editor modules.

1. Retriever

The process begins with the Retriever, which uses the BM25 algorithm to identify the most lexically similar code snippet from a large-scale retrieval corpus based on the input NL description.

2. Sketcher

The Sketcher is a 12-layer Transformer encoder that performs token-level classification. It distinguishes between "structural" tokens (relevant to the logic) and "detail-specific" tokens (irrelevant to the current requirement). Irrelevant tokens are replaced with <pad> tokens, resulting in a code sketch. To train this module, the system utilizes the Longest Common Subsequence (LCS) between the retrieved snippet and the ground-truth code as a supervisory signal.

3. Editor

The Editor is an encoder-decoder model that takes the NL description and the generated code sketch as input. It treats the sketch as a "soft template," filling in the <pad> placeholders with requirement-specific details to produce the final code. This separation allows the sketch to guide the model on how to write, while the NL prompt specifies what to write.

Experimental Evaluation

We evaluated SkCoder on three datasets: HearthStone (Python), Magic (Java), and a new large-scale dataset, AixBench-L (Java). AixBench-L contains 200k NL-code pairs with associated unit tests, enabling rigorous evaluation via the Pass@1 metric.

Key Results

  • Performance Gains: SkCoder significantly outperformed 20 baselines. Compared to CodeT5-base, it achieved improvements of 30.30% on HearthStone, 35.39% on Magic, and 29.62% on AixBench-L.
  • Functional Correctness: On AixBench-L, SkCoder reached a 20.00% Pass@1 rate, compared to 15.43% for CodeT5.
  • Model Agnostic: SkCoder is effective across various base models, improving their Pass@1 performance by up to 120.1%.
  • Human Evaluation: Manual assessment by experts confirmed that SkCoder generates code with higher quality, maintainability, and correctness than existing state-of-the-art methods like REDCODER.

Conclusion

SkCoder demonstrates that mimicking the human "Retrieve-Sketch-Edit" workflow is a powerful paradigm for code generation. By filtering retrieved code into structural sketches, the model avoids the pitfalls of literal copying and focuses on relevant patterns. Future work will explore dense retrieval methods for better semantic matching and end-to-end training to further optimize the synergy between the Sketcher and Editor modules.

Summary updated

BibTeX
@misc{li2023skcoder,
  author = {Jia Li and Yongmin Li and Ge Li and Zhi Jin and Yiyang Hao and Xing Hu},
  title = {{SkCoder: A Sketch-based Approach for Automatic Code Generation}},
  year = {2023},
  eprint = {2302.06144},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2302.06144},
}
Figure