10 December 2007 - 12:12TODOs in code considered harmful
Twice within the last week, I’ve found an easily preventable bug in my code. The problem, in both cases, was that I put a TODO comment in the code, fully intending to come back later and fix up whatever issue I put the TODO in for. If you’ve developed software for any amount of time, you know the rest of the story. I never went back in, completely forgot about the TODOs, and blissfully shipped the code. Over a year later, my forgotten TODOs came back to bite me in the form of bugs. Luckily for me in both cases the bugs were very minor - but still quite annoying.
TODOs in code should be considered harmful. They’re bugs waiting to happen, and they’re so easy to forget about. There’s a sort of pathology I see a lot in code in which the author hits a particularly sticky problem (or perhaps just something they can’t be bothered to do) and they just slap in a big TODO comment. For me, no more TODOs. They’re not visible enough, not noticeable enough, and they’re a lame excuse for not doing the right thing in the first place.
There are a couple of better alternatives to TODO comments. The first is to instead file a new bug against yourself to fix whatever issue you would have written the TODO for. This has much better visibility than TODOs. It also allows you to estimate, prioritize, and schedule the needed work. Depending on your tools, doing this will range from cumbersome to elegant. A very nice IDE/issue tracking integration feature would be to allow for creating new issues from within the IDE, in the context of a file that you’re editing.
Another alternative is to write some quick, naive code that is observably incorrect. Again, this has much better visibility than TODOs, since anyone using the software should be able to tell at a glance that things aren’t right. Throwing an exception would fit in this category, as long as the exception is observable to a user/tester of the software. However, in most cases you want to write code that actually works, but has some behavior that reminds everyone that additional work still needs to be done before the feature is complete. This allows testing / other work to progress, but doesn’t allow the issue to fall off the radar.
If you do testing or QA, here’s an easy way to find bugs. Take a look through your codebase for TODO comments. For each comment you find, determine if the comment is 6 months or older, if the author of the comment is no longer with the team, etc. For those comments that have fallen by the wayside, there’s an excellent chance you’ll uncover some bugs around them. Personally, as a developer, I am going to be much more disciplined about not using TODO comments in my own work.
Further reading:
Point: http://c2.com/cgi/wiki?TodoCommentsConsideredHarmful
Counterpoint: http://c2.com/cgi/wiki?TodoCommentsConsideredUseful
No Comments | Tags: Uncategorized