[Paper Review] SkCoder: A Sketch-based Approach for Automatic Code Generation
SkCoder is a sketch-based code generation framework that improves automatic code generation by mimicking human developers' reuse behavior: it retrieves a similar code snippet, extracts a relevant code sketch (a reusable pattern), and edits it to match the natural language requirement. The approach outperforms SOTA models by up to 30.30% on exact match and improves Pass@1 by up to 120.1% across multiple models and datasets.
Recently, deep learning techniques have shown great success in automatic code generation. Inspired by the code reuse, some researchers propose copy-based approaches that can copy the content from similar code snippets to obtain better performance. Practically, human developers recognize the content in the 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 the code sketches and tend to repeat the similar code without necessary modifications, which leads to generating wrong results. In this paper, we propose a sketch-based code generation approach named SkCoder 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". The post-editing further adds requirement-specific details to the sketch and outputs the complete code. We conduct experiments on two public datasets and a new dataset collected by this work. We compare our approach to 20 baselines using 5 widely used metrics. Experimental results show that (1) SkCoder can generate more correct programs, and outperforms the state-of-the-art - CodeT5-base by 30.30%, 35.39%, and 29.62% on three datasets. (2) Our approach is effective to multiple code generation models and improves them by up to 120.1% in Pass@1. (3) We investigate three plausible code sketches and discuss the importance of sketches. (4) We manually evaluate the generated code and prove the superiority of our SkCoder in three aspects.
Motivation & Objective
- To address the limitation of copy-based code generation models that repeat entire code snippets without necessary modifications.
- To model human developers' code reuse behavior, where only relevant parts (code sketches) are extracted and adapted.
- To improve code generation accuracy by combining retrieval, sketch extraction, and requirement-aware editing.
- To evaluate the effectiveness of code sketches across different models and datasets, including a new 200k-sample Java dataset.
- To demonstrate that sketch-based editing significantly improves code generation performance over direct copying.
Proposed method
- A retriever module selects a semantically similar code snippet from a corpus using a dense retrieval method.
- A sketcher module identifies and extracts only the relevant code parts from the retrieved snippet based on the natural language requirement.
- The extracted code sketch serves as a structural template for generating the target code.
- An editor module modifies the sketch by inserting requirement-specific details to produce the final code.
- The framework is integrated with various code generation models (e.g., CodeT5, GPT-based) to evaluate generalizability.
- The approach is evaluated using standard metrics (EM, BLEU, CodeBLEU, Pass@1, AvgPassRatio) on public and newly collected datasets.
Experimental results
Research questions
- RQ1Can a sketch-based approach outperform existing copy-based methods in code generation?
- RQ2How effective is the extraction of code sketches—specifically, what design choices for sketches yield the best performance?
- RQ3To what extent does the sketch-based editing mechanism improve code generation across different models?
- RQ4Does the proposed method generalize to diverse programming languages and code generation tasks?
- RQ5How does the inclusion of sketches affect model robustness and correctness, especially under unit test evaluation?
Key findings
- SkCoder outperforms the SOTA model CodeT5-base by 30.30%, 35.39%, and 29.62% on exact match across three datasets.
- On the newly collected AixBench-L dataset, SkCoder improves Pass@1 by 12.9% and AvgPassRatio by 8.49% over SOTA baselines.
- The ablation study confirms that adding the sketcher and retriever modules boosts Pass@1 by up to 120.1% across different models.
- Three distinct sketch design choices were evaluated, and the results confirm that sketch relevance and structure are critical for performance.
- Manual evaluation confirms that SkCoder-generated code is more correct, readable, and semantically aligned with requirements than baselines.
- The framework demonstrates strong generalization, improving diverse models including CodeT5, GPT-based, and encoder-decoder architectures.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.