A Powerful Lesson on Refactoring

My friend Steve Jones sent me a link to the video Practical Refactoring. I’ve long been a fan of refactoring, though it’s been a lot of years since I read Refactoring by Martin Fowler. It’s a long lesson, about 2 hours, so I watched in chunks as I had time. Basically they take a sample of working code that needs an enhancement, then step you through a  process of cleaning and refining a targeted segment of code as a way of learning the code, making it a bit better than before. There are some great lessons in the video:

  • Small improvements are entirely doable and worth doing
  • They ran a 2 minute time for many of the tasks, followed by a check-in. Time boxing is good, getting to a safe stopping point is good
  • Better doesn’t mean perfect
  • Good tools make a lot of difference
  • Simple and elegant is better than clever

Those don’t really capture it though. They spent a lot of time removing bad/not useful comments, to the point that I’m rethinking comments entirely. Part of that is there might be value to a “hide all comments” switch, part is that so many weren’t useful (and I’ve written a lot just like those) that I question the time investment. The comments were noise, and that’s not good. It’s worth watching just to think about that part of it.

Unit tests are what make practical refactoring possible. Minus the tests it can still be done, but with a lot less confidence. No, just because they pass doesn’t mean you didn’t break something, but it means you haven’t broken something obvious.

It’s code/.Net centric, and I’d say should be required watching for every developer, it’s that good. Listen to the questions, and the answers. These are common problems.

It also reminds me that our world – SQL – is much harder to refactor. We lack the tests, we can’t see all the dependencies, we rarely can identify public vs private procs (methods). That in turn makes me think about the challenges of DevOps, why programmers like ORM tools, and why, so far, none of those quite solve all of the problems we have.

Go watch a few minutes of it, see the rhythm, see how the tools help, see how small improvements can add up. Worth thinking on.