Header

DIY Refactoring for Scala

Excuse my long absence from blogging, but I have a good excuse: I’ve been hard at work on my Scala Refactoring project.

Today, I’m going to show you how you can implement your very own automated refactoring for Scala. “Implement a refactoring you say? But I don’t use IDE xy!”. Don’t worry, you won’t have to use a specific IDE, in fact, you don’t need to use an IDE at all.

Why should you want to write a refactoring? IDEs usually provide a bunch of general refactorings and code generators, but maybe you need a framework or project specific one that no IDE will implement for you. And don’t worry, it is not as complicated as it might sound. (And I should add that when I write refactoring, this includes all program transformations that affect the source code, so you could also create a transformation that just creates new code.)

A refactoring is essentially a transformation of the program in its tree form. Unfortunately, our programs are stored in plain text files, so the transformed tree has to be converted back to text, and this without losing all our pretty formatting. One of the design goals of the Scala Refactoring library was to separate these two concerns as good as possible, so that the implementor (you!) of a refactoring can concentrate on transforming trees and let the library do all the ugly code generation for him. To make it easier for those who already know the Scala compiler’s abstract syntax tree, the refactorings are completely based on this AST instead of introducing a new program representation. I can’t introduce Scala’s AST in detail here, but there’s an introduction in my term project’s technical report which I also plan to expand in my master’s thesis (please give me feedback if you notice any errors).

Please continue reading the rest of the post on my project wiki (where the layout is much more suited for source code).

4 comments »
Header

Scala Refactoring Term Project Finished

Today, after 15 weeks of hard work, I was finally able to hand in my Refactoring for Scala term project (its website is scala.ifs.hsr.ch, where you can also find the technical report). After a short break of four weeks, I will continue the project as my master’s thesis.

What has been achieved so far? After investigating the Scala compiler (nothing is more fun than learning a new language by reading it compiler’s source code, trust me), I started to create with a foundation to build the refactorings on. Refactorings basically just transform an AST, which has to be converted back to plain text afterwards (which is far more complicated than it sounds, trust me). This occupied me for the larger part of the project, and it still isn’t finished, but I was able to come up with a scheme that allowed me to leave it in an unfinished state and still being able to perform refactorings, at least good enough for a proof-of-concept.

The implemented refactoring is Extract Method; a perfect candidate because it is not too simple (with regards to the code transformatioms) and it looks much more impressive than e.g. Rename. Is it already usable? I’m not sure, it has not been tested with a lot of real world code (and usually when I did, it didn’t take me long to find new bugs, or rather things that were not yet implemented), but that will certainly be done in the near future. Another hurdle to using it is that you would need to install my modified Eclipse Scala plug-in (and I haven’t yet been able to create a working nightly-build including the Eclipse plug-in and all my stuff that would be needed).

For the near future, i.e. before the thesis, the plan is to implement organize imports, so I can bribe Miles Sabin into including the refactoring library in the Eclipse Scala IDE. After that, I’ll continue the project with a 20 week full-time thesis (~800 hours of work), where I hope to stabilize the existing Extract Method, advance the library so it can be used by other developers without knowing the inner workings of it, and to provide many new refactorings!

In the mean time, I highly appreciate any feedback. And keep an eye on this blog or follow me on twitter to hear the latest about the project.

10 comments »
Header

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.

9 comments »
« Older
Newer »