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?
Commenting accessors, especially if they don't do any operations anywhere, is unnecessary and a waste of fingertips.
If someone reading your code can't understand that
person.getFirstName()
returns the first name of a person, you should try everything in your powers to get him fired. If it does some database magic, throws a few dice, calls the Secretary of First Names to get the first name, It's safe to assume it's a non-trivial operation, and document it well.If, on the other hand, your
person.getFirstName()
doesn't return a person's first name... well, let's not go there, shall we?Absolutely pointless - you're better off without this kind of crap cluttering your code:
Very useful, if warranted:
Especially the explanation of what the property actually means can be crucial in domain models. Whenever I see a bean full of properties with obscure names that only investment bankers, biochemists or quantum physicists understand, and the comments explain that the setGobbledygook() method "sets the gobbledygook.", I want to strangle someone.
Ask yourself what do you want people to see when the comments are viewed as JavaDocs (from a browser). Many people say that documentation is not necessary since it's obvious. This won't hold if the field is private (unless you explicitly turn on JavaDocs for private fields).
In your case:
It's not clear what salary is expressed in. It is cents, dollars, pounds, RMB?
When documenting setters/getters, I like to separate the what from the encoding. Example:
The first line says it returns the height. The return parameter documents that height is in meters.
Why don't they just include a reference tag to let you comment the field value and the reference from getters and setters.
So that the documentation applies to the getter and setter as well as the field (if private javadocs is turned on that is).
If there isn't special operation in getter/setter I usually write :
With Javadoc (with private option):
and/or
With Doxygen (with private extract option):
If it is a getter/setter, it should be documented.
I digress here, but if it can be made a property, perhaps that is better for simpler coding of the users of the class. I digress further but as for all the comments that have "java" anywhere in them, who said it was java? (the tags, but the question could apply anywhere really)