Refactoring in the Dishwasher
Posted by Richard Mon, Dec 26 2005 21:26
In the past year, I was talking in a meeting and mentioned "refactoring" code is important. Most articles I have read cover why you should refactor or what you can do to make it feasible (automated unit testing, etc). In the meeting, someone asked what is refactoring. I was surprised since it has been such a hot topic in software development. The question came from a knowledgeable person, but their view was limited to developing and supporting a large ERP system. Unfortunately, this type of system doesn't lend itself to the concept of refactoring.
I wish I could say I had some revelation about how to refactor within an ERP system, but that is not what I am writing about. Today, I am writing about what my understanding of refactoring code is.
When programming, you have a great opportunity to modify the underlying logic of your application throughout the development process. In other building professions, like construction, once you build something, it can be more difficult to change it. If the foundation is weak or badly designed, the effort to fix it can be quite significant (even if it is something small like replacing a few bricks). In contrast when writing an application, you can modify the code with minimal cost when done correctly (of course, there are ways to minimize the chance of errors).
Back to the question, what is refactoring? When refactoring, you make changes to the structure of the code. An example might be loading the dishwasher. You might start with the plates all on one side of the dishwasher. You don't run the dishwasher immediately because it isn't full (especially if your area is in a drought and you are following the county's suggestion). Instead, you close the door and wait for more dishes. When you open it again and see the plates lined up on one side, you realize that it would make more sense to split the plates up to conserve space. Of course if this was software, you would have an automated test to verify that the position of the plates would still clean them affectively.
You might then decide to add a few things to the dishwasher. You then realize that putting one or two plates on the other side might give you enough space to add the big bowl you just used. You might also realize that it would be better to put a few of those dishes in the top drawer since they are smaller. Again, you are refactoring your dishes.
This might be an extreme example, but it hit me over the holiday (plus just finishing this book probably had me in the right frame of thought). With all loading of the dishwasher I've been doing, it reminded me of refactoring. Put a few dishes in, rearrange the next time you open it. Someone else might add a few dishes and move dishes around for a better fit. A continuous process until you run the dishwasher (finish a cycle of development) and then it starts all over again.
For better approaches and examples, there are several methodologies where refactoring is part of software development.
