Extract Method for Scala
I haven’t blogged in quite a while now. Actually, the last entry was to announce my term project on Scala Refactoring. My excuse is, I was hard at work! So without further ado:
class Demo1 { def demo1(i: Int): Int = { val a = i val b = a + i b } }
Select the line with the assignment to b, murmur the incantation press some keys, et voilà:
class Demo1 { def newMethod(i: Int, a: Int): Int = { val b = a + i b } def demo1(i: Int): Int = { val a = i val b = newMethod(i, a) b } }
It also works with multiple return values and when passing functions. Now, the code isn’t ready yet, and I have to concentrate on writing my report right now, so it might take a couple more weeks until I can ship something.
Et voilà!
Congrats on your achievement, looking forward to the report and the actual plugin.
Hey Mirko,
Very nice. Extract method is one of the refactorings I use the most, so good choice. 🙂 Have you thought about which you will attempt next? I think rename would be very nice (even if just local rename to start with). 😉
Best,
Ismael
Nice work!
Mirko, hi,
could you please clarify these points for me.
how exactly is this going to be integrated with any of the Scala plug-ins for the various IDEs. my understanding is that you’ll first have to compile the source file with scalac to get the resulting refactored file.
seeing the ‘after state’ of the transformation. will this be possible? many of the current refactorings allow me preview the resulting file(s).
i’ve just started playing about with compiler plug-ins so my knowledge is limited.
also, i agree with Ismeal — rename would be a good choice 😉 — and, all the best with your report.
Mirko,
upon reading
Max Schäfer, Mathieu Verbaere, Torbjörn Ekman, Oege de Moor. Stepping Stones over the Refactoring Rubicon — Lightweight Language Extensions to Easily Realise Refactorings. European Conference on Object-Oriented programming, ECOOP 2009.
I stumbled across this
“We would like to thank …and Dmitry Jemerov of JetBrains, who graciously gave us access to IntelliJ’s test suite.”
maybe that would be of great use to you and the Scala plug-in. I presume that it should be available, since they’ve open sourced their code.
If you decide to ask, it’d great to tell us the outcome.
Thank you all for your comments! I’ve been sick these past days, so please forgive my late replies.
Ismael: Yes, I guess rename (all kinds of renames) will be next, as a part of my master’s thesis, which starts in February.
Mohamed: Yes, the file needs to be compiled so I have an AST to work with. The result of a refactoring will be a bunch of “changes” (like a patch). This can then be previewed and applied by the user in the IDE.
Thank you for the reference, sounds interesting, I’ll take a look at it. There’s also the ASTGen project http://mir.cs.illinois.edu/astgen/ that generates ASTs to test refactorings, that might also be helpful.
Mirko, hi, when do you plan to make your code available?
My plan is to finish the term project this week, then I can clean up the website (Trac) and publish the whole thing.
Hallo Mirko,
I am really lucky to find your blog. It gave me an idea for one of my projects. You make my day.