Tuesday 11 May 2004 — This is 19 years old. Be careful.
When I look back on a week of work, and review the code changes I’ve made, there are often a number that are trivial changes to clean up code, often in far-flung areas. I am mocked for this habit. I sometimes mock myself for it. But I think it is important. I call it “pulling weeds”.
Andy Hunt and Dave Thomas, the Pragmatic Programmers, have said Programming is Gardening, and I have to agree with them. As good as their analogy is, they missed the topic of weeds.
One habit I have noticed in truly impassioned gardeners is that they are never idle in their garden. Whenever the opportunity arises, they will pull weeds. They may have headed out to replant, or to prune, or to feed, but while out, if they notice a weed, and they have a free hand, they will pull it.
I’m the same way with code. While pursuing weightier matters, if I notice little things wrong, I will fix them. “Acquire” is misspelled “aquire”. Fix it. A Windows-specific call has crept in where a portable one will do. Fix it. A function prototype has been orphaned after its definition has been removed. Fix it. The code formatting conventions aren’t being followed. Fix it.
This can be taken to absurd lengths, and I have taken it there (I’m remembering a phase where I was methodically working my way through a Java project, alphabetizing the imports). But the more typical extreme is for developers to believe that none of these changes matter, and to leave them be. Who cares about spelling errors? What difference does obsolete code make?
Gardeners pull weeds because they know that if left unchecked, the weeds will take over the garden, and that leaving it all until some optimistic cleanup day will make the job at least very difficult, and possibly never happen at all. Weeds in the code are the same way: they are not inert. They spread gradually but insidiously until removing them becomes a major job in itself.
To draw again from the Pragmatic Programmers, this is a case of broken windows. Small problems left uncorrected lead to lax attitudes about the bigger problems. Only by being diligent about all the problems in the code can you hope to truly nail them. The little ones may not matter in the scheme of things, but little ones become big ones, and the big ones do matter. Get them while they are small.
I agree with you. And I take the liberty to weed my own code regularly.
The biggest problem with weeding, though, is it adds a lot of noise to the version control, making it difficult to track significant changes. Alas, nothing ever comes for free.
I was going to make a similar point as Doug: the source control system, especially on a large project, can make it painful to do weeding on files that have a lot of people changing them. Ironically, these are often the files that need to be weeded most.
I think if you limit the scope of problems classified as "weed", this is an essential practice.
Strangely, I was weeding the other day and manage to mangle several of my real plants while doing it. Analogy abuse? Perhaps...
I well remember inheriting the maintenance of some sludgy Notes database several years back. I noticed that one of the ACL roles was mis-spelt thus: [KnowldegeMgrs].
Being a good chap, I decided to correct the typo. All hell broke loose, as code all over the damn NSF referenced the incorrectly-spelled version.
I had to put in a little overtime to fix this... ;o)
Add a comment: