Old Code in comments [closed]

2020-08-09 08:30发布

How long should you keep old code commented out in your code base? The contractors continue to keep old code in code base by turning it into comments. This is really frustrating and I want them to just remove the old code instead of commenting it out.

Is there a valid reason to keep old code in the code base as comments? I am using Version control by Visual Sourcesafe

2楼-- · 2020-08-09 08:42

Yeah nuke it on sight. It gives no value other that to show that the developer that commented it either wasn't sure about the removal of it. Or they don't know how to use your source control software.

3楼-- · 2020-08-09 08:42

So many reasons to leave the old code there. Basically - once it is deleted it is effectively gone - unless somebody actually remembers it was there.

As an evil contractor I don't delete code unless I am doing a substantial rewrite of a procedure - scrapping it and replacing it rather then "fixing" it.

ゆ 、 Hurt°
4楼-- · 2020-08-09 08:49

Keeping the old code around just makes it more difficult to read the code as a whole. As long as there is some form of version control in place for the project, the commented out code should be deleted. If you don't have revision control and can't set any up, then placing the old code in a file somewhere not part of the code base is advisable.

Emotional °昔
5楼-- · 2020-08-09 08:49

Those who say to get rid of commented-out-code because version control tools will solve any problem you might run into, are idiots.


Ever had to revise a modification because "it wasn't entirely bad, but alas it wasn't entirely good either" ? If you can still take out the production source and KNOW that the previous version of the code is still in there, in some textual form, it will save you a lot of time, because you will not need to resort to complex, difficult-to-control and therefore very error-pronce processes of using any "partial source merging" and "partial source consolidation" that your version control tool may offer you.

People who don't like this reality surely must have spent their whole careers producing only code that never was "not entirely bad, but not entirely good either", or in other words, producing only code that was either entirely bad or else entirely perfect. And we all know how big the probability is of achieving the latter.

6楼-- · 2020-08-09 08:51

Tell the contractors to stop doing this. This is a terrible practice.

There isn't really any reason to keep old code in the code base; it just gets in the way. Your version control system will show you the history of each file.

The only possibly good reason for keeping old code there is possibly for historical reference (perhaps if the old code did something particularly strange that might be relevant for the current situation).

Occasionally I will just comment out code knowing that I am putting a temporary change in (by temporary I mean less than a few days) and definitely plan to go back to it.

edit: another related practice is putting names and dates of changes in the file:

// 06/20/2009 - joe changed this #1245

Don't do this either. It might seem valuable at the time to see who made a change, but over time it really doesn't have any value and also clutters up the code.

7楼-- · 2020-08-09 08:51

I'm assuming you refer to significant code blocks that seem to be of some value, or good algorithms in their own right, not just the odd line here or there.

In these cases, there's a natural tendency to keep the code, if you've made large changes keeping the old as comments acts as a form of code review. Anyone getting the latest version will be able to instantly see the large changes that were made, and if there is a problem, its much more easily seen what used to be there. If the problem is with the new code, then there's a very simple way to instantly check it.

So, given that, I tend to comment out old code for the first revision, and then only delete it when the code is subsequently changed again, by this time the change will have been 'bedded in' and not likely to be a cause of bugs.

These comments are a form of documentation, there's no need to remove them for any purist ideal of 'clean' coding. Remove them when they are no longer needed, keep them while they may be of value.

登录 后发表回答