14 Aug 2009

While reading The Art Of Unit Testing, I was stuck on a particular sentence:

You should then remove (not comment out) the invalid requirement and its tests.

This made me think about something that happen very often: when modifying code, we are tempted to comment out some of the code that is not used anymore.

I suppose that we do that for two reasons:

  1. If we need to get something from that old code, it is easy to get it back as it is still there.
  2. Deleting code is a very hard psychological action for a developer. In Code Complete (well, I think it’s in there, I don’t have a copy here to confirm), Steve says that for a developer, throwing away code that he wrote is very difficult.

However, remember that as your source code is under version control (it is, right? If not, read this and this), so you should not worry about deleting stuff. If you accidentally delete something that you later want to get back, use undo or have a look at the previous version of the file. If some code is not used anymore, just get rid of it.

Comments should be there to help the next developer to understand the code. If you comment out a line of code but leave it because it makes the code more clear, that’s fine. Commenting a whole method that is not used anymore doesn’t help to understand the code, so it should be deleted.



blog comments powered by Disqus