Simple Getter/Setter comments

2019-01-12 17:01发布

What convention do you use to comment getters and setters? This is something I've wondered for quite some time, for instance:

/**
 * (1a) what do you put here?
 * @param salary (1b) what do you put here?
 */
public void setSalary(float salary);

/*
 * (2a) what do you put here?
 * @return (2b)
 */
public float getSalary();

I always find I'm pretty much writing the exact same thing for 1a/b and 2a/b, something like 1a) Sets the salary of the employee, 1b) the salary of the employee. It just seems so redundant. Now I could see for something more complex you might write more in the (a) parts, to give context, but for a majority of the getters/setters out there the wording is almost exactly the same.

I'm just curious if, for the simple getters/setters its ok to only fill in either the (a) part OR the (b) part.

What do you think?

15条回答
SAY GOODBYE
2楼-- · 2019-01-12 17:46

I always fill in both. The additional time spent typing is negligible, and more information is better than less, in general.

查看更多
贼婆χ
3楼-- · 2019-01-12 17:47

If the javadoc does not add anything, I delete the javadoc and use the auto-generated comments.

查看更多
倾城 Initia
4楼-- · 2019-01-12 17:49

This kind of boilerplate can be avoided by using Project Lombok. Just document the field variable, even if it's private, and let Lombok annotations generate properly documented getters and setters.

For me, this benefit alone is worth the costs.

查看更多
Anthone
5楼-- · 2019-01-12 17:52

I'm really disappointed about the answers basically saying comprehensive documenting is a waste of time. How are clients of your API supposed to know that a method called setX is a standard JavaBean property setter unless you say so clearly in the documentation?

Without documentation, a caller would have no idea whatsoever if the method had any side effects, other than by crossing their fingers about the apparent convention being followed.

I'm sure I'm not the only one here to have had the misfortune to find out the hard way that a method called setX does a whole lot more than just set a property.

查看更多
家丑人穷心不美
6楼-- · 2019-01-12 17:53

it is ok to fill in the (b) part, especially if you put a comment at the field declaration indicating what the field is all about.

查看更多
Melony?
7楼-- · 2019-01-12 17:55

Generally nothing, if I can help it. Getters and setters ought to be self-explanatory.

I know that sounds like a non-answer, but I try to use my time for commenting things that need explanation.

查看更多
登录 后发表回答