LAMBDAFICATOR: Crossing the gap from imperative to functional programming through refactorings



Problem Description

Java 8 will support lambda expressions and will extend the Collections APIs with functional operations like map or filter that apply a lambda expression over the elements of a collection. Refactoring existing code to use these new extensions enables explicit but unobtrusive parallelism and makes the code more succinct. However, refactoring is tedious (it requires changing many lines of code) and error-prone (the programmer must reason about the control-flow, data-flow, and side-effects). Fortunately, these refactorings can be automated. We present LAMBDAFICATOR, a tool which automates two refactorings. The first refactoring converts anonymous inner classes to lambda expressions. The second refactoring converts for loops that iterate over collections to functional operations that use lambda expressions. In 9 open-source projects we have applied these two refactorings 1263 and 1709 times, respectively. The results show that LAMBDAFICATOR is useful. First, 55% of anonymous inner classes and 42% of for loops pass the preconditions, thus the refactorings are widely applicable. Second, converting anonymous to lambda decreases the code size by 2213 lines while converting for loops expressed 2382 operations, thus the refactorings provide real value to the projects. Third, the tool saved the programmer from changing 16020 lines, thus it reduces the programmer's burden.

Here is a cool YouTube demo of some features.

Use Lambdaficator

LAMBDAFICATOR is implemented as a NetBeans refactoring plugin. The tool provides two main workflow options, a QuickHint option and a batch option. The quick hint option scans the file that is open in the editor in real-time. If LAMBDAFICATOR finds a valid conversion, it underlines the code and displays a hint in the sidebar indicating this annonymous inner class can be converted into a lambda expression. If the programmer clicks the hint indicator, LAMBDAFICATOR applies the refactoring. This option allows the programmer to perform the refactoring without deviating from her normal workflow. The batch option allows the programmer to invoke the refactoring automatically by selecting any file, folder, or project open in the IDE. LAMBDAFICATOR can automatically apply the refactoring on all files or optionally generate a preview which lists the valid conversions and provides finegrain control over which conversions should take place. With the batch option, LAMBDAFICATOR can discover and apply hundreds of conversions in a matter of seconds. LAMBDAFICATOR is available in the JDK8-lambda branch of NetBeans. Instructions to get and build the JDK8-lambda branch of NetBeans are found here. In order to be able to run any of the refactorings you will need to install the lambda branch of JDK8


Below is a screenshot of running the AnonymousToLambda refactoring in batch mode, identifying an anonymous inner class that can be refactored to a lambda expression.





Below is a screenshot of applying the ForLoopToFunctional refactoring in batch mode and identifying one loop that can be refactored to three operations on collections using lambda expressions.





Publications

More details about the tool, the algorithms and, experimental evaluation can be found in our FSE'13 research paper.

If you want to find out more, look at our ICSE'13 tool demo paper.

Acknowledgements

Lyle Franklin and Alex Gyori carried this research as undergraduate summer interns at the Information Trust Institute at the University of Illinois at Urbana-Champaign.

Feedback

For improving LAMBDAFICATOR we need your feedback. We would love to hear constructive criticism or just learn that you are using LAMBDAFICATOR. Please contact Danny Dig.