Sunday, November 30, 2008

Refactoring


When I was first introduced to Algebra, it didn't go well. Even though mathematics had always been an easy subject for me - producing straight A grades - there was something about Algebra that just didn't click. Despite my best efforts to do well at it, I failed the subject and had to retake it the following year. Fortunately, the following year was different - it clicked, and I was back to making straight A grades again.

Learning Algebra was like exploring a whole new world in mathematics - I was fascinated by it. There was one aspect of Algebra that had me hooked - reducing equations. I had a natural talent for evaluating complex equations and reducing them down into their simplest form. I thoroughly enjoyed doing it, I was very good at it, and I couldn't get enough of it. It was like crack.

When I became a computer programmer, I found a new (and similar) natural talent - refactoring. I would look at long, complex computer code and I would reduce it to its simplest form. In procedural languages I would create function libraries, and in object oriented languages I would reorganize classes and class methods. All of it came naturally and without any formal training in the subject.

I always wondered if I was doing it correctly and if there were other methods I could learn, so I picked up Martin Fowler's book on refactoring. It was worth reading. I agree with him that bad code has a smell and that some refactorings come naturally. I now have names for the methods I've used in the past, and I also have new methods I never thought of. After reading the book, I looked over some of the code I refactored recently and I was pleased to learn I did it "right" - there was nothing suggested in the book that would make the code simpler or better organized. I'm not trying to boast, I'm just saying it feels good to know for sure.

...

Sometimes I find myself getting frustrated while working on a software project. When that happens, I put the project on the shelf for a while and do something fun to occupy my time until I'm ready to get back to it. What's the "fun" thing I end up doing most often? Refactoring smelly code. I can't help it. It's like crack.

No comments: