Daily Quick Tip : Avoid the Clutter
The word clutter can be defined as:
a large amount of things that are not arranged in a neat or orderly way : a crowded or disordered collection of things
What really caught my attention in this definition was the term disordered. For something to be disordered, it first had to have an order. To disorder something is to disrupt the systematic functioning or neat arrangement of it.
I have often seen codebases fall into a state of clutter, moving from an orderly state to a disorderly state. I think the biggest reason for this is that we have a hard time letting go. We think, “I may need this in the future”.
I’ve often seen…
- developers write code that isn’t immediately needed
- developers commenting out code
- developers renaming things, only to replace them by another implementation of the same original name
The first bullet is simply a failure to embrace the YAGNI principle (You Aren’t Gonna Need It). It happens because the programmer thinks it might be needed down the road. Ron Jeffries wisely states, “Always implement things when you actually need them, never when you just foresee that you need them.”
The last two are symptoms of the same desire: we might want to revive the code later, or refer back to it for some other purpose. So, we keep it around. (And this doesn’t happen just in code, I’ve even seen it happen in databases as well — and what a mess that becomes!)
Further reducing these three bullets, I believe it all boils down to fear. Fear that I might miss something. Fear that I might forget how to do something. Fear that I might break something.
Fear is a powerful force. Most sane people don’t think it’s a good idea to allow fear to dictate our actions, but as programmers we often allow it to dictate our coding practices.
I think the best way to avoid clutter, moving from orderly to disorderly, is to let go of fear.
Think you might need something? Don’t want to forget it? How about jotting down a note on an index card, or in your favorite note taking software, to remind yourself about it? Don’t implement it, just note it down enough for you to recall your thought processes later. If you read that note later and you can’t figure out what you were thinking, it may be indicative that it was a bad idea to begin with (or that you need to improve your note taking skills).
If you’ve found something in code that you no longer need, delete it. Assuming you’re using an SCM, and committing your code regularly, you’re not likely to lose it forever.
Of course, having a good test suite makes deleting code more feasible and can increase your comfortability. But even without a good test suite (*gasp*) you can still discipline yourself to let go of the fear. What’s the worse that can happen? You might have to write code again? That doesn’t seem like a bad thing to me; you’ll probably even do it better the second or third time.