What Builds Effective In-Context Examples for Code Generation?

Dongze Li, Songqiang Chen, Jialun Cao, Shing-Chi CheungarXiv 2025

TL;DR

This paper deconstructs In-Context Learning (ICL) for code generation to discover exactly which features of a code example drive performance. Through systematic ablation, the authors reveal that semantically meaningful variable and function names are the single most critical factor, while code formatting matters much less. Crucially, they find that current LLMs fail to learn from solutions to similar problems, exposing a major bottleneck in their ability to reason by analogy.

What Problem Does This Paper Address?

In-Context Learning (ICL)—prompting a Large Language Model (LLM) with a few examples of a task—is highly effective for code generation. However, researchers typically treat these code examples as monolithic blocks. We haven't known what actually makes an example "good." Is it the precise indentation? The choice of keywords? Or the descriptive variable names?

Understanding these fine-grained contributions is essential for optimizing prompt engineering and designing better Retrieval-Augmented Generation (RAG) systems for software development. Furthermore, the paper tackles whether LLMs can perform "reflection-driven" learning—meaning, can an LLM look at a solution to an analogous problem and infer how to solve a new, target problem?

Methods

The authors designed a controlled ablation framework using mutation operators to systematically strip specific features from ground-truth code examples before feeding them into the prompt.

Figure 1: Pipeline to Collect Solution Code as ICL Example for RQ1 and RQ2

They categorized code features into three dimensions:

  1. Code Naming: Obfuscating function names (Function Ordered Erosion - FOE) or variable names (Variable Ordered Erosion - VOE) into generic symbols like var0 or entry_function.
  2. Code Format: Removing indentation (INR), removing line breaks (LSR), or converting the code into a raw JSON list of tokens (TOK).
  3. Implementation Details: Converting code into natural language descriptions (NL), swapping loop types (LT), or replacing code with high-level pseudo-code (PC).

To evaluate how naming styles affect LLMs, they also tested precise names, verbose names, and random high-frequency variable names. Finally, to test analogical reasoning, they built a RAG system to fetch structurally similar problems using sparse retrievers (BM25), dense embedding models (gist-large, jina-v2-code, codesage-small), and human-annotated problem tags.

Figure 2: An Example of ICL-Enhanced Prompt. The Target Question Contents and Start Code parts follow the official prompt template of LiveCodeBench. The ICL question con- tent refers to the identical question content in RQ1-2, and similar question content in RQ3.

Datasets

The study evaluated models using a subset of 362 LeetCode questions from the LiveCodeBench benchmark. To ensure the findings were language-agnostic, the tasks were evaluated in both Python and Java. For the RAG experiments, they utilized a database of 2,659 Python solutions and 2,353 Java solutions to pull similar examples.

Key Insights and Ideas

  • Semantics Trump Structure: LLMs rely heavily on the semantic cues embedded in variable names. If you rename a variable from counter to var0, the model's ability to leverage that example drops drastically. Conversely, stripping function names barely hurts performance, as models easily infer intent from the function body.
  • Language Familiarity Breeds Format Robustness: LLMs are surprisingly robust against formatting destruction in Python. Even when Python code was completely flattened into a JSON token array, models like GPT-4o-mini could still extract the logic. However, they were highly sensitive to formatting disruptions in Java, suggesting that format robustness correlates with how deeply a language is represented in the training data.
  • The Analogy Blindspot: Current LLMs completely fail at reflection-based learning. Providing an LLM with a perfectly solved similar problem does not help it solve the target problem; in fact, it often acts as a distraction, yielding results worse than zero-shot prompting.

Key Results

The primary evaluation metric used was pass@1.

Table 1: Code Generation Performance (Pass@1) with ICL Examples Mutated by Different Operators on Code
  • The Naming Penalty: Eliminating descriptive variable names (VOE/FVE) caused the most severe performance drops—up to 30 percentage points for Qwen2.5-7B-Instruct on Python tasks.
  • Format Sensitivity: Tokenizing code (TOK) caused a minor drop in Python but up to a 12-point drop in Java for GPT-4o-mini.
  • The Failure of Analogy: When providing solutions to similar questions (via RAG or human annotations), performance consistently hovered around or fell below the Zero-Shot baseline. For example, GPT-4o-mini achieved a 39.78% pass@1 on zero-shot Python, but dropped to 38.38% when provided with an expert-annotated similar question and solution.
Table 2: Code Generation Performance (Pass@1) with ICL Examples Using Different Naming Styles in Code

Limitations and Future Work

The authors note that LeetCode problems are self-contained, algorithmic, and standalone. Real-world software engineering involves complex, multi-file repositories with intricate dependencies, which this study does not capture.

Additionally, the study was limited to four models (GPT-4o-mini, Qwen2.5-7B/32B, and DeepSeek-Coder-V2-Lite). The authors suggest that future work should focus on improving LLMs' abstract reasoning capabilities, perhaps by exploring how structural code representations (like Abstract Syntax Trees) can be used in prompts to help models successfully learn from code analogies.

Summary updated

BibTeX
@misc{li2025what,
  author = {Dongze Li and Songqiang Chen and Jialun Cao and Shing-Chi Cheung},
  title = {{What Builds Effective In-Context Examples for Code Generation?}},
  year = {2025},
  eprint = {2508.06414},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2508.06414},
}
Figure