Skip to main content
QUICK REVIEW

[Paper Review] What Makes Good In-context Demonstrations for Code Intelligence Tasks with LLMs?

Shuzheng Gao, Xin-Cheng Wen|arXiv (Cornell University)|Apr 15, 2023
Software Engineering ResearchComputer Science57 references5 citations
TL;DR

This paper investigates how to design effective in-context demonstrations for code intelligence tasks using large language models (LLMs). It systematically evaluates the impact of demonstration selection, ordering, and number of examples across three tasks—code summarization, bug fixing, and program synthesis—showing that optimized demonstrations can improve performance by up to 175.96% in F1 score over standard methods.

ABSTRACT

Pre-trained models of source code have gained widespread popularity in many code intelligence tasks. Recently, with the scaling of the model and corpus size, large language models have shown the ability of in-context learning (ICL). ICL employs task instructions and a few examples as demonstrations, and then inputs the demonstrations to the language models for making predictions. This new learning paradigm is training-free and has shown impressive performance in various natural language processing and code intelligence tasks. However, the performance of ICL heavily relies on the quality of demonstrations, e.g., the selected examples. It is important to systematically investigate how to construct a good demonstration for code-related tasks. In this paper, we empirically explore the impact of three key factors on the performance of ICL in code intelligence tasks: the selection, order, and number of demonstration examples. We conduct extensive experiments on three code intelligence tasks including code summarization, bug fixing, and program synthesis. Our experimental results demonstrate that all the above three factors dramatically impact the performance of ICL in code intelligence tasks. Additionally, we summarize our findings and provide takeaway suggestions on how to construct effective demonstrations, taking into account these three perspectives. We also show that a carefully-designed demonstration based on our findings can lead to substantial improvements over widely-used demonstration construction methods, e.g., improving BLEU-4, EM, and EM by at least 9.90%, 175.96%, and 50.81% on code summarization, bug fixing, and program synthesis, respectively

Motivation & Objective

  • To address the critical gap in understanding how to construct high-quality in-context demonstrations (ICL) for code intelligence tasks with large language models.
  • To investigate the impact of demonstration selection, ordering, and number of examples on ICL performance in code-related tasks.
  • To provide actionable, evidence-based guidelines for researchers and practitioners to improve ICL effectiveness in code summarization, bug fixing, and program synthesis.
  • To demonstrate that optimized demonstrations significantly outperform random or heuristic-based baselines, especially in low-resource settings.
  • To validate the generalizability of findings across multiple LLMs and code datasets, while identifying limitations in current evaluation scope.

Proposed method

  • Conducted extensive empirical experiments on three code intelligence tasks: code summarization, bug fixing, and program synthesis using three LLMs.
  • Evaluated multiple demonstration selection strategies, including random selection, similarity-based retrieval (using BM-25 and semantic embedding), and diversity-based selection.
  • Compared different ordering strategies: random, similarity-based (descending), and reverse-similarity (ascending) ordering of examples.
  • Varied the number of demonstration examples (from 1 to 10) to assess the impact on model performance across tasks.
  • Used standard metrics such as BLEU-4, Exact Match (EM), and F1 to evaluate performance, with statistical significance testing.
  • Proposed a systematic framework for demonstration design based on empirical findings, integrating selection, ordering, and example count optimization.
Figure 1 : An example of in-context learning on code summarization task.
Figure 1 : An example of in-context learning on code summarization task.

Experimental results

Research questions

  • RQ1What kind of selection methods are most effective for in-context demonstrations in code intelligence tasks?
  • RQ2How does the ordering of demonstration examples affect ICL performance in code-related tasks?
  • RQ3How does the number of demonstration examples influence the performance of ICL in code intelligence tasks?
  • RQ4How generalizable are the findings across different LLMs and programming languages?

Key findings

  • Similarity-based selection using semantic embeddings (e.g., SBERT) significantly outperforms random or BM-25-based retrieval for demonstration selection.
  • Ordering demonstrations by descending similarity to the input query improves performance, with reverse-similarity ordering often underperforming.
  • The optimal number of demonstration examples varies by task, with 5–8 examples generally yielding peak performance, though performance plateaus beyond 6–8 examples.
  • A carefully designed demonstration strategy—combining optimal selection, similarity-based ordering, and appropriate example count—improved EM by 175.96% on bug fixing, 50.81% on program synthesis, and 9.90% on code summarization over baseline methods.
  • The performance gains from optimized demonstrations are consistent across multiple LLMs, indicating strong generalizability of the findings.
  • The study reveals that ICL performance is highly sensitive to demonstration design, and naive selection or random ordering leads to suboptimal results even with large models.
Figure 2 : Illustration of design space of in-context demonstrations.
Figure 2 : Illustration of design space of in-context demonstrations.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.