Ever felt the satisfaction of a doctor just finished with an intense surgery. I have... quite often after finishing a particularly broad and complex refactoring. The sense of accomplishment when reviewing the resulting clean code can be quite invigorating.
This is why I am a big fan of test-driven development. After all, having the code covered by unit-tests provides me with the safety net that enables me to perform the boldest and most effective refactorings possible to achieve clean easy-to-maintain code.
However, despite having unit-tests, can refactoring get unwieldy? Sometimes, when the code has not been cleaned up for a while and a huge refactoring is necessary. What is one way to prevent that? I'd say, it's the home-cleaning metaphor.
During my college years, I knew a friend who had a very hard time keeping his apartment clean and tidy. That is because he always pushed the task till later in favor of study or entertainment. It wasn't until his apartment became totally unbearable that he finally cleaned it or asked for help, and by that time, cleaning became an extremely dreadful task.
I knew another friend who had a very clean and tidy apartment. Cleaning his home was a non-issue because he did it everyday in very small increments whenever something became out of order.
Code refactoring is very similar in the sense that if it is done in small increments everyday whenever code-smells are noticed, it makes it easy to maintain a clean and tidy code-base.
What's a good pattern that can help achieve that discipline? The paragraph-summary metaphor. When doing technical writing, authors often begin with a draft, and then refine it incrementally to summarize the content until it is concise and easy to understand.
This same pattern can be applied with code. Whenever you finish writing a piece of functionality, review it and refactor it to ensure it is DRY, and then repeat the process until you are satisfied with the code structure and cleanliness.
If you have other suggestions on how to keep a code-base clean through refactoring, please share in the comments area.
No comments:
Post a Comment