1 / 21
CodeHint: Dynamic and Interactive Synthesis of Code Snippets Joel - - PowerPoint PPT Presentation
CodeHint: Dynamic and Interactive Synthesis of Code Snippets Joel - - PowerPoint PPT Presentation
CodeHint: Dynamic and Interactive Synthesis of Code Snippets Joel Galenson, Ras Bodik, Koushik Sen UC Berkeley 1 / 21 Motivation Specification Test cases Desired type 2 / 21 Pictures by Mangpo Phitchaya Phothilimthana CodeHint:
2 / 21
Motivation
Pictures by Mangpo Phitchaya Phothilimthana
Test cases Desired type Specification
3 / 21
CodeHint: Autocomplete for the modern age
- Autocomplete is useful but very limited.
- Our improvements:
– Being dynamic – General specifications – Synthesis
4 / 21
Overview
CodeHint Source code Test case Specification Candidates Find locals Evaluate Candidates Combine Refine
5 / 21
Example: The problem
6 / 21
Example: First step
“Find a JMenuBar”
7 / 21
Demo
8 / 21
Basic algorithm
- Find local variables, do BFS over Java
statements, show user those that pass spec.
– Actually evaluate these statements, including file
I/O, reflection, etc.
x y Iteration 1 x + y x max y Iteration 2 Iteration 3 x max y average x + y ... ... ... ...
9 / 21
First iteration
- Find local variables.
x y tree null this ...
10 / 21
Second iteration
- Get each expression's type and combine it with
- thers in type-correct ways.
x x + y x < y ... tree (JTree) getPathForLocation x tree y (JTree) getPathForLocation y tree x tree getTopLevelAncestor ... tree getWindowAncestor SwingUtilities ...
11 / 21
Side effects
- Handle in-memory and external effects.
makeTree() add tree children ...
renderer makeTree()
... tree tree.children changed from to
renderer renderer makeTree()
file delete SecurityManager Breakpoints In-memory: External:
12 / 21
tree getTopLevelAncestor getJMenuBar
Equivalence classes
- Group equivalent code to avoid unneeded work.
tree getRoot SwingUtilities tree getTopLevelAncestor tree getTopLevelAncestor getName tree getRoot SwingUtilities getName tree getRoot SwingUtilities getJMenuBar tree getTopLevelAncestor getJMenuBar Next iteration: End of search:
13 / 21
Probabilistic model
- Mined 10MLOC to build model of likely code.
- Use to avoid unlikely expressions.
Probability of accessing method/field m on type T: Probability of using constant c as argument i to method m: tree getNextMatch ... KeyEvent.VK_ENTER getComponent tree
14 / 21
Third iteration and result
tree getTopLevelAncestor (JFrame) getJMenuBar (JTree) getRowForLocation x tree y + 1 ... Third iteration: Result: (JFrame) getJMenuBar tree getRoot SwingUtilities tree getTopLevelAncestor (JFrame) getJMenuBar
15 / 21
Refinement
- Users can give another demonstration in a
different state to refine the results.
(JFrame) getJMenuBar tree getWindowAncestor SwingUtilities tree getTopLevelAncestor (JFrame) getJMenuBar ...
16 / 21
Synthesis from specifications
Strength
Weak Strong
Context- dependence
Context- independent Context- dependent API exploration Program synthesis Programming by demonstration Static type rv instanceof JMenuBar Correctness condition isSorted(array) Value demonstrations x == 42 x.toString().contains(“Eve”)
17 / 21
Empirical results
In real-world code, ~95% of expressions need ≤3 iterations and ~99% need ≤4.
Our algorithms work well in practice.
18 / 21
User studies
- Completed two user studies with 28 subjects.
- Found statistically-significant productivity
improvements.
– Fewer bugs and more tasks completed in less time.
CodeHint makes programmers more productive.
Lower is better Higher is better
19 / 21
Future work
- Improving the probabilistic model
- CodeHint for JavaScript
– https://github.com/jgalenson/codehint.js –
- Integrating symbolic techniques
20 / 21
Probabilistic search
foo ; bar tree getWindowAncestor SwingUtilities ... Model Candidates tree getWindowAncestor SwingUtilities (JFrame) getJMenuBar
21 / 21
Summary
- Dynamic and interactive synthesis
- Autocomplete for the modern age
- User studies showed productivity improvements