TL;DR
This paper introduces Scotch, an in-IDE semantic code search engine that leverages surrounding code context to improve search relevance. By fine-tuning CodeBERT and introducing a new large-scale dataset, Scotch demonstrates that incorporating code context significantly enhances the ability to find relevant code snippets, even for ambiguous queries.
What Problem Does This Paper Address?
Finding relevant code snippets is a crucial task for developers, especially with the ever-growing volume of code available. Traditional code search engines often rely on keyword matching or simple pattern matching, which can struggle with the inherent ambiguity of natural language queries. Furthermore, developers typically have to leave their Integrated Development Environment (IDE) to perform these searches, disrupting their workflow. While semantic code search has emerged to better understand code meaning, it hasn't fully addressed the contextual nature of code development. Developers often write code with an implicit understanding of the surrounding code, which is lost in traditional search. This paper tackles the problem of making code search more effective and integrated into the developer's workflow by introducing "contextual code search" – leveraging the code already present in the IDE to disambiguate and improve search results.
Methods
The core of Scotch is built upon the CodeBERT model, a pre-trained model that understands both natural language and programming languages. The authors adapt CodeBERT for code search in two ways:
- Baseline (CodeBERT without context): A natural language query is fed into CodeBERT, along with a token indicating the programming language. The model is trained to maximize the similarity between the query's representation and the target code snippet's representation.
- Contextual Code Search (CodeBERT with context): This is the novel approach. The natural language query is prepended with the preceding code context from the current file. This combined input is then fed into CodeBERT. The model is trained to maximize the similarity between this context-query representation and the target code snippet.
For efficient searching over a large database of code snippets, Scotch employs ScaNN (Scalable Nearest Neighbors), an approximate nearest neighbor search algorithm. This allows for sub-linear time querying, making real-time search feasible. Separate ScaNN indexers are built for each programming language supported.
Datasets
The paper introduces a new, large-scale dataset called the Scotch dataset.
- Scotch Dataset: This dataset comprises approximately 19 million functions collected from open-source GitHub repositories with permissive licenses. Crucially, each function is paired with its code context (the code preceding it in the same file). About 4 million of these functions also have corresponding docstrings, which are used as natural language queries.
- Manually Curated Test Set: To rigorously evaluate the code ranking quality, the authors also created a smaller, manually curated test set. This set contains around 957 annotations across four programming languages, with relevance scores assigned by human annotators.
The Scotch dataset is notable for being the first large-scale, openly available dataset specifically designed for contextual code search.
Key Insights and Ideas
- Context is King (for retrieval): The most significant insight is that incorporating code context dramatically improves the retrieval of the correct code snippet. This is especially true for ambiguous or underspecified queries, where the surrounding code provides the necessary clues.
- IDE Integration Boosts Productivity: By operating directly within an IDE, Scotch eliminates the need for developers to switch contexts, streamlining the search process and enhancing productivity.
- Contextual Search Enables Underspecification: Developers can intentionally provide less detailed queries when using Scotch, as the context can fill in the gaps, making the search experience more natural.
- Slight Trade-off in Ranking Quality: While context significantly helps find the right snippet, it can slightly degrade the ranking of the top results compared to a baseline without context, particularly on manually curated test sets. This suggests that models might need further refinement to perfectly balance retrieval and precise ranking.
Key Results
- MRR Improvement: The contextual CodeBERT model achieved significantly higher Mean Reciprocal Rank (MRR) scores across all programming languages compared to the baseline. For example, MRR Avg was nearly 9% higher, and MRR All was 6% higher. Javascript saw the largest improvements.
- NDCG Degradation: On the manually curated test set, the NDCG score for the contextual model was slightly lower (1.8% on average) than the baseline. This highlights the trade-off mentioned above.
- Human Evaluation: Human evaluators preferred the contextual model's predictions over the baseline in three out of four programming languages, further validating the benefits of context.
- Recall@k: Figure 3 shows that the contextual model consistently outperforms the baseline in Recall@k, especially for lower values of k, indicating it's better at finding relevant snippets within the top results.
Limitations and Future Work
- Ranking Quality: The paper acknowledges that the inclusion of code context slightly impairs the model's ability to rank annotated code snippets. The authors hypothesize that selectively using important aspects of code context might improve ranking and suggest this as a direction for future work.
- Ambiguity in NDCG Analysis: The authors analyzed NDCG score trends but couldn't find consistent patterns to explain the decrease. Further investigation into how code context affects ranking is needed.
- Expanding Language Support: While Scotch currently supports four languages, the modular design of ScaNN indexers makes it easy to add support for more programming languages in the future.
- User Feedback: The system includes a user feedback mechanism, which the authors plan to use to improve future versions of Scotch.
Summary updated