TL;DR
This paper introduces "program splicing," a novel approach to automate code completion by leveraging a vast database of existing code snippets. By allowing programmers to write "drafts" with natural language comments and test cases, the system intelligently synthesizes missing code, aiming to significantly boost programmer productivity and reduce the pitfalls of traditional copy-pasting.
What Problem Does This Paper Address?
Programmers frequently resort to copying and pasting code from online resources to accelerate development. While efficient, this practice is notorious for introducing bugs due to manual modification and a lack of understanding of the copied code's context. The challenge is to capture the benefits of this common workflow—leveraging existing code—while mitigating its risks and improving its precision. Existing synthesis tools often struggle with underspecification or require extensive manual guidance, leaving a gap for a more integrated and data-driven approach.
Methods
The core of the proposed solution is "program splicing," a two-stage synthesis process:
- Code Search: Given a programmer's "draft" (incomplete code with natural language comments and correctness requirements), the system queries a large database of existing code snippets (over 3.5 million Java methods in their implementation). This search is guided by features extracted from the draft, including natural language terms and function/variable names, using a weighted k-nearest neighbor approach.
- Hole Substitution: Once relevant code snippets are retrieved, the system extracts "codelets" (expressions and statements) and uses an enumerative search to systematically fill the "holes" (represented by
??) in the draft. Heuristics like type matching and role matching are employed to prune the search space and improve efficiency. The process is iterative, allowing programmers to refine their drafts and invoke further synthesis rounds.
Datasets
The primary dataset used is a corpus of over 3.5 million Java methods extracted from the Sourcerer code repository. This forms the basis for the code search and retrieval component. The evaluation uses a suite of everyday programming tasks, including implementations of scripts, modifications of algorithms, and prototypes of software components.
Key Insights and Ideas
- Program Splicing as an Enhanced Copy-Paste: The core idea is to formalize and automate the intuitive process of finding and integrating relevant code from external sources, making it more robust than manual copy-pasting.
- Data-Driven Synthesis: Leveraging a massive corpus of real-world code biases the synthesis towards human-readable and common-sense solutions, addressing the underspecification problem inherent in traditional synthesis.
- Iterative and Interactive Process: The system is designed to work interactively with the programmer, allowing for multiple rounds of synthesis and modification, mirroring a realistic development workflow.
- Importance of Natural Language: Natural language comments in the draft play a crucial role in guiding the code search, highlighting the synergy between human intent and automated code retrieval.
- Heuristics for Efficiency: Techniques like type matching and role matching are vital for managing the combinatorial explosion of the search space, making the synthesis practical.
Key Results
- Performance Improvement: The system demonstrated significant speedups in completing programming tasks compared to a "without splicing" baseline, with average completion times being substantially lower for most problems.
- High Success Rate: Program splicing successfully completed all benchmark problems, often with a single invocation of the system, indicating its ease of use and effectiveness.
- User Study Validation: A user study with 18 participants showed that program splicing significantly boosted programmer productivity, with "with" tasks being completed faster than "without" tasks.
- Comparison with µScalpel: The system outperformed µScalpel, a state-of-the-art transplanting system, on comparable benchmarks.
- Code Search Precision: The authors' code search method showed competitive or superior precision compared to GitHub's search engine, especially for smaller numbers of retrieved neighbors.
Limitations and Future Work
- Database Quality: The quality of the synthesized code heavily depends on the quality of the programs in the database. Ensuring high-quality database programs is an ongoing challenge.
- Syntactic Support: The system's ability to handle code with syntactic features not supported by its internal representation is limited.
- Repetitive/Irrelevant Programs: The presence of repetitive, irrelevant, or invalid programs in large code corpora can negatively impact search precision.
- Synthesizing Statements: Synthesizing sequences of statements is noted as a more difficult task than synthesizing expressions due to the lack of strong contextual cues.
- Future Work: The authors suggest developing better features and similarity metrics for code search to improve precision, and exploring more sophisticated ways to handle the quality of database programs. They also mention the potential for adding more selection functions to choose the best solution among multiple completions.
Summary updated