RSS RSS feed | Atom Atom feed

New strategy for commented code

After looking over loads and loads of code, Ive come to the conclusion that commenting out dead code is often done incorrectly. Typically you will see something like this.

public void doSomething(String myString) {
    //for(int i=0; i<10; i++) {
    //  callSomething();
    //}
}

This is all wrong. First you never know when this was commented out. Secondly, its hard to tell who commented it out. The whole point of leaving this code is for documentation purposes right? This kind of documentation doesn't seem complete now does it? Im suggesting that the programmer insert his/her initials and the date that these lines of code were tagged defunct. Im not telling you anything new, am I? Didn't think so. Actually, the more that I think about it, that is a pretty crappy way to handle the situation!

Any true software professional would not do such a thing. Its ridiculous to leave such crud scattered in source code. If the project you are working on has no version control software, then download CVS. Not only is it free and easy to install, its one of the most widely used CM tools around. Virtually every open source project is managed using it. If you don't like CVS, pick any other version control tool that suits your fancy. By doing so, you give yourself the safety net of always being able to get back to where you were previously. Just about everyone understands this fundamental concept, so why does defunct code exist? Somebody somewhere must be getting paid by the size of source files, that is the only reasonable solution.

    // If anyone has an explanation or other ideas, please drop me a line.

Tags :


Re: New strategy for commented code

> Somebody somewhere must be getting paid by the size of source files, that is the only reasonable solution. Well, I worked for a company once in which the manager judged the work done by doing a "wc -l "! She was stupid and obviously I had to leave soon. However even there I couldn't dream of leaving commented out code to increase line count. I think the reason people do it is because they think they will need it soon (how soon?). And they are not fully comfortable with leveraging source control systems. In fact I have seen the same guys also do very-big-checkins-which-often-leave-a-trail-of-disaster-behind. Personally what worked for me is to delete the code and write an exhaustive comment in CVS indicating that the following code-segement (which did this and was located in this method) has been deleted because... Almost never did I need to go back to such methods though. Which probably means that we might be better off in just deleting it, making sure all the tests run and live with it.

Re: New strategy for commented code

One thing Ive learned is that predicting the future(i.e. when you will need something) is an unrealistic effort. Cleaner code IS easier to digest and MAY lead to less bugs in the future. That would be the only prediction I would make. And if Im wrong, then so be it. No harm done.

Add a comment Send a TrackBack