Saturday, September 13, 2008

Towelie: IRB Refactoring

The intended use case for Towelie is refactoring in IRB. Success! I just used it in IRB to identify several duplicated method definitions in a large Rails project.

Scanning 27 controllers, 84 models, and 20 helpers, Towelie identified 14 duplicated methods. Towelie doesn't yet have the ability to identify methods which are very similar, so when I say it caught 14 duplicated methods, I do mean methods which were exact duplicates. (Although Towelie is whitespace-agnostic, so they may not have been character-for-character duplicates.)

When I ran Towelie against the entire project, I got some kind of ParseTree error. It's possible this is due to a weakness in ParseTree, but more likely that ParseTree doesn't know what to do when an automated parsing process asks it to parse JavaScript or HTML. I'm planning to tweak Towelie to make it a little more careful about what it asks ParseTree to parse. In the meantime, if you want to run Towelie against some legacy code, I recommend you start by testing individual folders.

That's what I did. Here's my IRB command history:

294: require 'lib/array'
295: require 'lib/towelie'
296: include Towelie
297: puts duplicated "../[client's app name]/app/controllers"
298: puts duplicated "../[client's app name]/app/models"
299: puts duplicated "../[client's app name]/app/helpers"


Towelie then printed out each duplicated method, right there in IRB.