TL;DR
This paper introduces SNIFF, a novel search engine for Java that allows developers to query code snippets using natural language, overcoming the limitations of existing tools that require prior API knowledge. SNIFF achieves this by annotating existing Java code with Javadoc descriptions and then using a type-based intersection of candidate code snippets to deliver concise and highly relevant results.
What Problem Does This Paper Address?
Developing software often involves reusing existing libraries and APIs. However, the sheer volume and complexity of modern APIs, like Java's Standard Library with its thousands of classes and tens of thousands of methods, present a steep learning curve for developers. Discovering the right APIs and understanding how to combine them to perform a specific task can be a significant challenge.
Current approaches to code discovery fall into two main categories:
- Manual Exploration: Developers sift through existing code bases and their documentation, which is time-consuming and often infeasible due to the size of these code bases and the lack of comprehensive documentation.
- Existing Search Tools: Tools like Google Code Search allow free-form queries but return a lot of irrelevant information mixed with useful code snippets, requiring manual filtering. Other tools, like Prospector, are more targeted but require the user to already know the specific classes and object types they need to use, which is a barrier for developers unfamiliar with an API.
This gap highlights the need for a system that combines the flexibility of natural language queries with the precision of targeted code snippet retrieval, without demanding prior API knowledge from the user.
Methods
SNIFF employs a multi-stage approach to address the code search problem:
- Preprocessing and Annotation:
- Parsing Open-Source Java Code: SNIFF starts with a large collection of publicly available Java source code.
- Annotation with Javadoc: The core novelty here is automatically annotating client code (user-written code) with Javadoc descriptions. For statements containing method calls, SNIFF appends the corresponding Javadoc description of that method. This process enriches otherwise undocumented code with plain English meaning.
- Intermediate Representation: The annotated code is transformed into a simplified intermediate representation to facilitate indexing.
- Indexing: The annotated code is indexed in a database. This involves storing individual statements and creating a mapping from keywords (derived from comments and method names, after stemming and stop-word removal) to statement IDs.
- Querying and Retrieval:
- Database Search: When a user submits a free-form English query, SNIFF processes it (stop-word removal, stemming) and searches the database for code chunks that contain all the query keywords.
- Code Chunk Enhancement: To provide context, a few lines of code before and after the keyword-matching region are included.
- Intersection and Pruning:
- Type-Based Intersection: This is a key contribution. SNIFF performs a "type-based intersection" of the retrieved code chunks. The intuition is that relevant statements will be syntactically similar across different chunks, while irrelevant statements will differ. The system computes the Longest Common Subsequence (LCS) of statements based on type equivalence. This process effectively prunes irrelevant code and yields concise, highly relevant snippets.
- Algorithm: The LCS is computed using a modified dynamic programming algorithm, with a complexity of O(mn), which is manageable given the small size of typical code chunks.
- Clustering and Ranking:
- Clustering: Similar code snippets (those whose intersection is "valid" – meaning it still contains all query keywords) are grouped into clusters.
- Ranking: Clusters are ranked based on their "support," which is defined as the number of occurrences of the snippet across the client source files. Clusters with higher support are considered more relevant.
Datasets
- Codebase: The system was trained and evaluated on a large collection of Java source code, including Eclipse framework APIs, code from O'Reilly Java programming books, and the JGraph library. The paper mentions indexing around 500K lines of code.
- User Queries: Queries were collected from two sources:
- A set of programming problems designed for a user study.
- Frequently asked questions from a Java user forum.
Key Insights and Ideas
- Annotating with Javadoc is Powerful: The core idea of using Javadoc to annotate otherwise undocumented user code is a clever way to inject semantic meaning into code, making it searchable by natural language.
- Type-Based Intersection for Precision: The type-based intersection algorithm is crucial for filtering out noise and delivering truly relevant code snippets, distinguishing SNIFF from tools that return raw, unfiltered code chunks.
- Free-Form Queries are Essential: The ability to query using natural language, without needing to know specific API names or types, significantly lowers the barrier to entry for code reuse.
- Frequency as a Relevance Indicator: Ranking snippets by their frequency of occurrence (support) is a practical heuristic for identifying commonly used and thus likely relevant code patterns.
Key Results
- User Study: Developers using SNIFF solved programming problems up to 40% faster compared to Prospector and Google Code Search.
- Online Search Engine Comparison: SNIFF returned the most relevant snippet as the top-ranked result for approximately 87.5% of queries, significantly outperforming Google Code Search (25%), Koders (62.5%), and Krugle (12.5%).
- Intersection/Clustering Effectiveness: Turning on intersection and clustering improved the rank and reduced the size of the most relevant snippets. Intersection alone reduced snippet size by 34% on average, leading to less post-processing.
Limitations and Future Work
- Limitations:
- The effectiveness of the intersection algorithm relies on the assumption that relevant statements are similar across code chunks, while irrelevant ones differ. This might not always hold true.
- The current implementation focuses on Java.
- The paper acknowledges that while SNIFF returns the exact code snippet, users might still need to "play around a little with their queries" to refine results.
- Future Work: The paper suggests that future work could explore more sophisticated methods for query understanding and snippet ranking. The authors also mention that their intersection approach has similarities with DECKARD, which focuses on identifying similar subtrees, hinting at potential cross-pollination of ideas.
Summary updated