Java – Handling Unused Methods Highlighted by Code Coverage

javatest-coverageunit testing

I have been tasked to increase code coverage of an existing Java project.

I noticed that the code coverage tool (EclEmma) has highlighted some methods that are never called from anywhere.

My initial reaction is not to write unit tests for these methods, but to highlight them to my line manager/team and ask why these functions are there to begin with.

What would the best approach be? Write unit tests for them, or question why they're there?

Best Answer

  1. Delete.
  2. Commit.
  3. Forget.

Rationale:

  1. Dead code is dead. By its very description it has no purpose. It may have had a purpose at one point, but that is gone, and so the code should be gone.
  2. Version control ensures that in the (in my experience) rare unique event that someone comes around later looking for that code it can be retrieved.
  3. As a side effect, you instantly improve code coverage without doing anything (unless the dead code is tested, which is rarely the case).

Caveat from comments: The original answer assumed that you had thoroughly verified that the code is, beyond doubt, dead. IDEs are fallible, and there are several ways in which code which looks dead might in fact be called. Bring in an expert unless you're absolutely sure.