Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Commenting code that is removed [closed]

Tags:

comments

People also ask

Should you remove commented out code?

Removing commented code not only makes it harder to find later, but it also makes it so people in the future don't know it existed before. For finding the code, there are tools and git commands to help you look at the history of a file.

Why you shouldn't comment your code?

Writing and then maintaining comments is an expense. Your compiler doesn't check your comments so there is no way to determine that comments are correct. You are, on the other hand, guaranteed that the computer is doing exactly what your code is telling it to.

What does comment out the code mean?

Comment-out definition (programming) To temporarily disable a section of source code by converting it into a comment.


Generally, code that is removed should not be commented, precisely because it clutters the codebase (and, why would one comment on something that doesn't exist?).

Your defect tracking system or source control management tools are where such comments belong.


There are some (rare) situations when commenting code out (instead of deleting) is a good idea. Here's one.

I had a line of code that seemed good and necessary. Later I realized that it is unnecessary and harmful. Instead of deleting the line, I commented it out, adding another comment: "The line below is wrong for such and such reason". Why?

Because I am sure next reader of the code will first think that not having this line is an error and will try to add it back. (Even if the reader is me two years from now.) I don't expect him to consult source control first. I need to add comment to warn him of this tricky situation; and having wrong line and the reason why it is wrong happened to be the best way to do so.


I agree that it is not a good idea to leave code removed in comments.

Code history should be viewed through a version control system, which is where old code can be found, as well as the reason it was removed.


You should delete the code always.

As for being able to see old/removed code, that's what revision control is.


Depends on the reason for removal.

I think of comments as hints for people maintaining the code in the future, if the information that the code was there but was removed can be helpful to someone maintaining the code (maybe as a "don't do that" sign) then it should be there.

Otherwise adding detailed comments with names and dates on every code change just make the whole thing unreadable.


I think it's pretty useless and make the code less readable. Just think what it will be like after some monthes....

// removed because of this and that
/* 
      removed this stuff because my left leg...
*/
 doSomething();
// this piece of has been removed, we don't need it...

You'll spend half an hour to find out what's going on


The question is, why do you remove code?

Is it useless? Was it a mistake to put it there in the first place?

No comments needed from my point of view.