skip to main | skip to sidebar

Friday, November 9, 2007

Dirty code? Clean it up! Refactor!

Coding is a subjective science. I'm sure most of you have written code under pressure that just didn't feel quite right or perhaps had a design evolve under new requirements and realized the design was less than ideal. Some have spoken of code smells. I've always thought of code in terms of dirty and clean. When critiquing others I tend to refer to code and design as either 'feeling clean' or 'not feeling clean' as it is a little more politic.

Dirty code in an application is like a baby with a dirty diaper:
1. you know its time for a change.
2. if you put it off too long you'll have a big mess on your hands.
3. eventually there'll be more changes needed.

The answer to dirty code is refactoring, which is the process of changing code to improve readablility or structure without changing its functionality. Automated tools may be available to help with some refactoring depending on your development environment. A good introductory resource for best practices, including refactoring is Code Complete 2 by Steve McConnell. For an in-depth look at the topic I recommend Refactoring by Martin Fowler.