Ajarn Mark Caldwell Blog

Bringing Business Sense to the IT World…

Clean Up After Yourself

I have been editing some ASP.NET pages lately and finding a LOT of code that is either all commented out, or worse, someone created a way to permanently hide it from the user (such as ASP Panel that is never made visible) yet left all the code active.  Another trick: commenting out all the lines inside of a routine, but leaving the declaration intact to prevent code from crashing.  If you are intending to retire the routine, then kill it completely, don't just cripple it.  (I know that to some of you, the thought of completely killing a routine is blasphemy, it would "break the contract" and prevent backwards compatibility, yada, yada, yada...  That may be true in your world.  But in my current work world, we have complete control over our code's usage (or at least the authorized usage), and can actually retire code that is no longer used.)

So let me take a moment to encourage all developers to clean up after themselves as much as they can.  Don't be afraid to delete lines of code, instead of just commenting it out, especially when you have a good source code control system that you could recover from.  Oh sure, I understand commenting out a few lines while you try a different approach, but once you have successfully tested your new approach, clean out the old junk.  One code-behind file I was working on recently, I was able to strip down to 1/3 its previous size.  Half of the lines I removed were large blocks of commented out code or routines that were no longer being called by anything in the system, having been replaced by something more efficient, but nobody bothered to retire the old routines.  The other half of the lines I removed were due to very simple refactoring by extracting big blocks of code that were repeated in several places into a handful of reusable routines.

And while I'm on the subject of cleanup, don't be afraid to delete entire pages or code files that are no longer used.  Your source code control system should allow for that and for maintaining a consistent history path (as well as recovery if necessary).  By the way, if you're not happy with your current source control system (or ALM solution, for those who like TLAs), then I highly recommend SourceGear Fortress.  And you should take the time to read Eric Sink's article series on Source Control HowTo.  Even if you are a grizzled veteran, I'd wager that you'll be reminded of a few things you knew, but forgot.

So please, clean up after yourself.  Honor the repository and don't leave junk code laying around.