Tuesday, January 04, 2005

Refactoring in small steps

In our current refactoring we are moving the business logic from a "Date Effective" object to a snapshot. Unfortunately, the first attempt at creating the Snapshot was unsuccessful, leaving business logic in two places, and the system being tightly coupled with both the snapshot object and the backing master object.

Zach and I are trying to tease the two apart, and this requires changing the contract on the backing master object, so that no one can call it and all the non date-effective logic can be moved into the snapshot. The first thing we did was identify the contract and minimize the number of other object that were allowed to play with the master, they were forced to go through the snapshot.

We followed a couple of setps in Java to change the contract - forcing object to deal with the snapshot rather than the master object.

1) Each method we wished to removed from the contract we made protected

2) We found all references to that method, which were now broken and forced them to go through the snapshot if possible. Occasionally, this was not possible as a few methods would have a large impact on other pieces of code - we deferred those.

3) After each method change run all tests, repeat

There were then two cases two deal with to move the method to the correct class...
i) The method already existed in the snapshot. We made sure it delegated to the master. We found all the methods that referenced the one to be moved, and sent it to the one on the snapshot. We then inlined the master method.

ii) The method existed only on the master. We added the snapshot as a parameter to the method, and then moved the method to the snapshot.

All in all pretty tidy.


Post a Comment

<< Home