TL;DR
This paper introduces EM-Assist, a novel tool that combines Large Language Models (LLMs) with static analysis to automate the "Extract Method" refactoring process. By filtering LLM hallucinations and enhancing suggestions with IDE capabilities, EM-Assist significantly improves the accuracy and usefulness of refactoring recommendations, making AI assistants a practical reality for developers.
What Problem Does This Paper Address?
Long methods in software are notoriously difficult to understand, debug, and maintain. The "Extract Method" (EM) refactoring is a common solution, involving moving a block of code into a new, smaller method. While Integrated Development Environments (IDEs) can perform the mechanical aspects of this refactoring, choosing which code to extract has remained a challenge. Existing tools often suggest refactorings that don't align with developers' actual practices or preferences, leading to low adoption rates. This gap arises because refactoring is both a science (following principles) and an art (requiring developer intuition and context). LLMs, trained on vast amounts of code, offer a promising avenue for generating more human-aligned suggestions, but they suffer from "hallucinations" – plausible-sounding but incorrect or useless suggestions. This paper aims to bridge the gap between LLM potential and practical, developer-accepted refactoring.
Methods
EM-Assist employs a multi-stage approach to tackle the EM refactoring problem:
- LLM-Powered Suggestion Generation: The process begins by prompting LLMs (like GPT-3.5, GPT-4, and PaLM) with the code of a long method and instructions to suggest EM refactorings. This is done using "few-shot learning" with example demonstrations to guide the LLM.
- Hallucination Filtering: This is a critical step. EM-Assist uses static analysis techniques to:
- Remove invalid suggestions: Code fragments that would result in compilation errors or break code scope are discarded.
- Remove not-useful suggestions: Suggestions that extract the entire method body or just a single line are filtered out, as these are generally not beneficial for code renovation.
- Suggestion Enhancement: Valid and useful suggestions are further refined.
- Program Slicing: Techniques are used to adjust the extracted code fragment. For instance, if a variable used within the fragment is declared just before it, the fragment is expanded to include the declaration, avoiding the need to pass it as a parameter. Conversely, if a fragment starts with a control statement (like
if), it's shrunk to exclude the condition, keeping the condition visible in the host method for clarity. - Ranking and Prioritization: To avoid overwhelming developers, suggestions are ranked based on a combination of "heat" (how often a line of code appears in suggestions) and "popularity" (how often a suggestion is generated across multiple LLM runs).
- IDE Integration: Finally, EM-Assist integrates with IntelliJ IDEA to safely apply the developer-selected refactoring.
Datasets
The paper uses two datasets for evaluation:
- Community Corpus: A publicly available dataset of 122 Java methods and their corresponding EM refactorings, previously used by other researchers. This corpus includes both theoretical scenarios and synthetic refactorings.
- Extended Corpus: A newly created, larger dataset comprising 1752 actual EM refactorings mined from 12 open-source projects using the RefactoringMiner tool. This corpus aims to provide a more robust and realistic evaluation by focusing on refactorings actually performed by developers.
Key Insights and Ideas
- LLMs are prolific but flawed: LLMs can generate a large number of refactoring suggestions, but a significant portion (up to 76.3%) are "hallucinations" – either invalid or not useful. This highlights the necessity of post-processing LLM outputs.
- Static analysis is crucial for LLM validation: Combining LLMs with static analysis is key to filtering out incorrect suggestions and ensuring code integrity.
- Developer intuition matters: Refactoring is not purely algorithmic. LLMs, by learning from real-world code, can capture developer preferences better than traditional static analysis tools alone.
- Context is king: Enhancements like program slicing that consider variable usage and control flow are vital for generating suggestions that developers will find practical and useful.
- Ranking is essential for usability: Presenting developers with a curated, ranked list of high-quality suggestions is critical for adoption.
Key Results
- Improved Recall: EM-Assist achieved a recall rate of 53.4% on the Extended Corpus (actual developer refactorings), significantly outperforming previous best-in-class tools (39.4%). On the Community Corpus, it achieved 63% recall.
- High Developer Agreement: In firehouse surveys, 81.3% of industrial developers agreed that EM-Assist's suggestions were beneficial and suitable for their code.
- LLM Effectiveness: GPT-3.5, GPT-4, and PaLM all demonstrated effectiveness in generating suggestions, with GPT-3.5 yielding the highest number of useful suggestions (average of 10 per host method).
- Contribution of Components: An ablation study showed that EM-Assist's enhancements and ranking mechanism significantly improved recall, boosting it by up to 26 percentage points compared to raw LLM output.
Limitations and Future Work
- LLM Dependence: The effectiveness of EM-Assist is tied to the capabilities of the underlying LLM. As LLMs evolve, EM-Assist can be adapted.
- Language Specificity: The current implementation is tailored for Java and Kotlin. Extending it to other languages is a future direction.
- Project-Level Context: EM-Assist currently focuses on individual methods. Future work could extend its scope to suggest refactorings for reuse at higher levels (file, module, project).
- Developer Preferences: The tool could be improved by better accounting for developer preferences regarding method size and the potential for reuse.
Summary updated