EM-Assist: Safe Automated ExtractMethod Refactoring with LLMs

Dorin Pomian, Abhiram Bellur, Malinda Dilhara, Zarina Kurbatova, Egor Bogomolov, Andrey Sokolov, Timofey Bryksin, Danny Dig

TL;DR

This paper introduces EM-Assist, an IntelliJ IDEA plugin that leverages Large Language Models (LLMs) to automate the "Extract Method" refactoring. By combining LLM-generated suggestions with IDE-based validation and enhancement, EM-Assist significantly improves the accuracy and usability of automated refactoring, aligning better with real-world developer practices.

What Problem Does This Paper Address?

Modern software development often grapples with "long methods" – functions that grow too large and complex, making them difficult to understand, debug, reuse, and maintain. The "Extract Method" refactoring is a standard technique to break down these large methods into smaller, more manageable ones. While Integrated Development Environments (IDEs) support applying this refactoring, they typically leave the crucial step of identifying which code fragments to extract entirely to the developer. Existing automated tools often struggle to replicate human developers' nuanced decision-making, leading to suggestions that don't align with real-world refactoring patterns, thus limiting their adoption. This gap between automated suggestions and human intuition is a significant barrier to effective software maintenance.

Methods

EM-Assist tackles this problem by integrating LLMs into the refactoring workflow. The core process involves:

  1. LLM-Powered Suggestion Generation: The plugin prompts an LLM (specifically, GPT-3.5-turbo in their implementation) with the code of a long method. The LLM, trained on vast amounts of code, generates multiple suggestions for code fragments to extract. To mitigate LLM non-determinism, multiple iterations of the same prompt are performed.
  2. Validation and Enhancement: Crucially, EM-Assist doesn't blindly trust LLM output. It employs program slicing techniques to validate the syntactic correctness and usefulness of the LLM's suggestions. Invalid or non-useful suggestions (e.g., extracting the entire method body) are filtered out.
  3. Ranking and Presentation: To avoid overwhelming users, EM-Assist ranks the valid suggestions based on their frequency across multiple LLM calls. This assumes that frequently suggested fragments are more likely to represent important functional concerns. The top-ranked suggestions are then presented to the developer in a user-friendly popup window within the IDE, complete with previewed method signatures and highlighted code fragments.
  4. Safe Execution: Once a developer selects a suggestion, EM-Assist uses the IDE's robust refactoring framework to safely apply the change, ensuring compilation and correctness.

Datasets

The paper describes two main sources of data:

  • Real-world Refactoring Dataset: The authors mined the version history of open-source projects using RefactoringMiner to identify 1,752 actual "Extract Method" refactorings performed by developers. This dataset was used to quantitatively evaluate EM-Assist's effectiveness by comparing its suggestions against these real-world instances. This approach is highlighted as a key differentiator from prior work that often used smaller, synthetic datasets.
  • Usability Survey Participants: 18 industrial software developers participated in a qualitative evaluation of EM-Assist's usability.

Key Insights and Ideas

  • LLMs Mimic Human Intuition: The core idea is that LLMs, trained on vast real-world code, are better at capturing the subtle, context-dependent decisions human developers make during refactoring compared to traditional static analysis tools.
  • Validation is Crucial: LLMs can be prolific but also prone to "hallucinations" (syntactically incorrect or nonsensical suggestions). EM-Assist's strength lies in its robust validation and enhancement pipeline that filters out these errors.
  • Frequency as a Ranking Signal: Using the frequency of LLM suggestions as a ranking mechanism is an ingenious way to leverage the LLM's output to prioritize the most likely useful refactorings.
  • IDE Integration for Safety: By performing the final refactoring within the IDE, EM-Assist ensures the safety and correctness of the automated process, bridging the gap between suggestion and application.

Key Results

  • Effectiveness: EM-Assist achieved a recall rate of 53.4% among its top-5 suggestions when replicating 1,752 real-world "Extract Method" refactorings. This significantly outperforms the previous best-in-class tool (JExtract), which had a recall rate of 39.4%. Statistical analysis confirmed EM-Assist's superiority.
  • Usability: A survey with 18 industrial developers showed overwhelmingly positive results. 94.4% rated the additional workflow stages as convenient and easy to handle, and 94.4% liked the suggested method names. The popup window for suggestions was found to be helpful by 77.7% of respondents.

Limitations and Future Work

  • LLM Dependency: Currently, EM-Assist requires sending the user's host method code to OpenAI's servers. This raises concerns about proprietary code and potential privacy issues. The authors suggest future work could involve on-premise LLM deployment or using smaller, local models.
  • Response Time: While acceptable by usability standards (averaging 2 seconds), the LLM invocation can introduce latency.
  • Extensibility: EM-Assist currently supports Java and Kotlin. The authors note that extending it to other languages (like Python) or other IDEs would require modifications to specific steps of the workflow but that the core LLM-based approach is transferable.
  • Future Refactorings: The authors are actively working on expanding EM-Assist to support many more types of refactorings beyond "Extract Method."

Summary updated

BibTeX
@inproceedings{pomian2024em,
  author = {Dorin Pomian and Abhiram Bellur and Malinda Dilhara and Zarina Kurbatova and Egor Bogomolov and Andrey Sokolov and Timofey Bryksin and Danny Dig},
  title = {{EM-Assist: Safe Automated ExtractMethod Refactoring with LLMs}},
  booktitle = {SIGSOFT FSE Companion},
  year = {2024},
  doi = {10.1145/3663529.3663803},
}
Figure