Stephen Freeman Rotating Header Image

April, 2005:

Tetris or Simulated Annealing

I was working with a colleague on cleaning up an ugly corner of our codebase. We weren’t quite sure of the best solution, so we were doing some exploratory refactoring. The session was getting a little tense until we discovered that we had different approaches to this activity. Once we’d clarified this, we picked one and things went smoothly.

My colleague preferred to flatten the code out, putting back some duplication we’d removed earlier, and then look for alternative refactorings that lead in a better direction. This might be described as “simulated annealing”:http://www.nist.gov/dads/HTML/simulatedAnnealing.html, moving away from a local minumum to find a better one.

My habit is to chip away at the code by finding little duplications until larger patterns become visible; think of the lines disappearing in Tetris. As a technique, it probably requires more of an act of faith that things will work out in the end.

*Conclusions* _(i)_ There’s always another approach that you haven’t used yet. _(ii)_ It can take a while to calibrate with your colleagues.