Tuesday, November 25, 2008

The Golden Rules for Software Refactoring

How do we actually go about refactoring? Below are the Golden Rules.

  1. One change at a time
  2. Small steps
  3. Test after each step
  4. Frequent builds

These four steps are the key principles which you should keep in mind when doing any refactoring. If you remember nothing else about the technical aspects of refactoring, this is the list to remember. It is the mantra which during refactoring you should continually chant to yourself. It should be one of the post-its pasted to the side of your computer screen. Tie them to your hands as a reminder; write them on the doorposts of your house; wear them on your forehead....OK, now I’m getting a bit carried away.

But they are pretty important stuff. They are the key to good refactoring.
We are only going to make one change at a time, only one clearly defined improvement to the code. This single change we are going to try and do in small steps, often the smallest steps possible. Then we are going to test. If the test goes all right, then we are going to make another single change, again in small steps, and then we are going to test again.

In future posts I plan to look at each of these in more detail.

See my book The Refactoring Workout for more on refactoring.

No comments: