Convert to Sibling Refactoring

Recently the company I work for has been doing a lot of work in the web app framework Seaside. Up to this point we’ve done all of our green field projects in Rails. However, our president has a Smalltalk background and we’d both been impressed by what we’d seen of Seaside. We settled on a VisualWorks, Linux, PostgreSQL platform and went to work.

So recently I came across a refactoring that I often did in Ruby, etc. and was pleasantly surprised how quick and easy it was to do in VisualWorks. So the scenario looks like this: I create a class, find that I need another class that does most of the same stuff and would fit nicely into an inheritance hierarchy. To accomplish this, I’d often copy the class wholesale and change the bits that were different so I could see how much these things actually had in common. So from there, the goal of my refactoring would be to create a superclass for the two, put the common methods in that superclass and have the differences in the children. Sometimes I’d throw some exceptions in the methods I want the children to implement in the superclass. In Ruby though there seems to be less reason to do this, as a NoMethodError is nearly as explanatory as anything else you could raise. However in VisualWorks creating a new class allows you to auto-generate methods that require implementation by an ancestor. Anyway, I digress.

VisualWorks makes this refactoring easy with Convert to Sibling and Push Up. Simply execute the Convert to Sibling refactoring on one of your classes which creates a superclass with every method throwing a subclass responsibility error. Then in your subclass you can execute Push Up refactorings on the methods you want to exist in your superclass. All that’s left is to take your second class, change the parent to your new superclass and remove the methods the superclass implements.

Handy!

~ by Anthony on October 8, 2007.

Leave a Reply