How to explicitly comment empty methods?

12.7k views Asked by At
public void destroy() {
}

I have written above empty method in my filter class which is required as per the interface. But when my code goes to PMD check it mentions

Uncommented Empty Method finds instances where a method does not contain statements, but there is no comment. By explicitly commenting empty methods it is easier to distinguish between intentional (commented) and unintentional empty methods.

I do not understand it. What really does it mean?

4

There are 4 answers

2
chrylis -cautiouslyoptimistic- On

When you have a method with an empty body, it is possible that you started writing the method and then forgot to insert the body. If you intend for the method to be empty, you should make a note (in a comment) to explain that it's empty on purpose. In the case of Filter, since you have to implement the destroy() method, you should write a comment that says something like "nothing to clean up".

3
No Idea For Name On

the warning pretty much explain itself.

when you leave an empty method, you should leave a comment saying why is it empty, who did it and when/who is responsible to write it. that way users know why is it here and who should use it, and why isn't it working

0
Dan King On

It means - in PMD terms - that you have to leave a comment inside an empty method, so it's obvious that the method is meant to be empty.

You can comment the outside of the method as well if you like, but this won't satisfy PMD.

0
friederbluemle On

In certain cases it might only add additional clutter to a class if you had to add the same comment to each empty method body (for example if you're implementing an interface with a lot of methods, but you are only interested in using one method).

Use this annotation to suppress the warning:

@SuppressWarnings("PMD.UncommentedEmptyMethodBody")