Rewriting the Code: A Simple Method for Large Language Model Augmented Code Search

Haochen Li, Xin Zhou, Zhiqi Shen

Abstract

Code search is a fundamental task for developers, yet it remains challenging due to the inherent "modality misalignment" between natural language descriptions and source code implementations. While recent Generation-Augmented Retrieval (GAR) frameworks attempt to bridge this gap by generating exemplar code from queries, they often suffer from a stylistic mismatch between the generated code and the target codebase. This paper introduces ReCo (Rewriting the Code), a novel framework that leverages Large Language Models (LLMs) to normalize the style of the entire codebase. By searching for LLM-generated exemplar code within an LLM-rewritten database, ReCo ensures stylistic consistency, leading to substantial improvements in retrieval performance—up to 35.7% in Mean Reciprocal Rank (MRR).

Introduction

The primary hurdle in code search is that human-authored queries and source code are written in fundamentally different styles. A developer might search for "count elements in a list," while the optimal code snippet might use collections.Counter or a manual for loop. Traditional retrieval methods, even those augmented by LLM-generated code, often fail because the generated "exemplar" code does not stylistically match the ground-truth code in the database. This "style mismatch" creates a performance ceiling for existing search engines.

The ReCo Framework

To address the style mismatch, we propose ReCo (Rewriting the Code). The framework operates through a dual-augmentation process:

  1. Query Augmentation: For a given natural language query, an LLM generates an exemplar code snippet that represents a likely implementation.
  2. Codebase Normalization: Instead of indexing the original, heterogeneous code, ReCo uses an LLM to "rewrite" every snippet in the database into a standardized style. This is achieved via a summarize-then-generate approach: the LLM first generates a one-sentence summary of the code's logic and then produces a new implementation based solely on that summary.

By aligning both the query and the database to the LLM's preferred coding style, the retrieval task becomes a comparison between stylistically similar entities. This process effectively transforms the codebase into a new modality that is optimized for LLM-augmented search.

The ReCo framework aligns the style of the query's exemplar code with a rewritten version of the codebase to improve retrieval.

Quantifying Style: Code Style Similarity (CSSim)

A key contribution of this work is the introduction of Code Style Similarity (CSSim), a metric designed to quantify stylistic alignment. Unlike traditional metrics like BLEU or CodeBLEU, which focus on exact token matches, CSSim evaluates structural similarity using Tree Edit Distance and considers variable naming and API choices. Our analysis shows that CSSim is a much stronger predictor of retrieval performance than traditional text-matching metrics.

Correlation between various metrics and retrieval performance (ΔMRR). CSSim shows a strong positive correlation, while traditional metrics like ROUGE-L can be misleading.

Experimental Results

We evaluated ReCo across four datasets (CoNaLa, MBPP, APPS, and MBJP) using both sparse (BM25) and dense retrieval models. The results demonstrate significant and consistent improvements:

  • Sparse Retrieval (BM25): Achieved the highest gains, with MRR increasing by up to 35.7%.
  • Dense Retrieval: Zero-shot models improved by up to 27.6%, while fine-tuned models saw gains of up to 23.6%.
  • Cross-Model and Cross-Language Generalization: The benefits of ReCo held true across different LLMs (e.g., GPT-3.5, Code Llama) and across both Python and Java.

Notably, the resurgence of sparse retrieval suggests that many 'semantic' gaps in code search are actually 'lexical' gaps caused by stylistic variation. When style is normalized, keyword-based matching becomes significantly more robust.

Discussion and Limitations

One of the most striking findings is that with ReCo, simple sparse retrieval methods can outperform sophisticated dense models that have not undergone style normalization. This suggests that "style" is effectively its own modality, and aligning styles is as important as aligning semantics.

However, ReCo faces practical challenges regarding scale. Rewriting a massive codebase like GitHub is computationally expensive. Future research should focus on developing smaller, more efficient "style-normalization" models to make this approach viable for industrial-scale applications. Additionally, while the codebase can be rewritten offline, generating the exemplar code for the user's query still adds latency to the search process.

Conclusion

ReCo demonstrates that addressing stylistic misalignment is a powerful lever for improving code search. By transforming the codebase to match the LLM's generative style, we can significantly enhance the effectiveness of both simple and complex retrieval systems. This work highlights the importance of style as a first-class citizen in the design of future code search engines.

Summary updated

BibTeX
@inproceedings{li2024rewriting,
  author = {Haochen Li and Xin Zhou and Zhiqi Shen},
  title = {{Rewriting the Code: A Simple Method for Large Language Model Augmented Code Search}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2024},
  doi = {10.18653/v1/2024.acl-long.75},
}
Figure