views:

1651

answers:

13

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 salary();

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?

A: 

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.

akf
No good - people don't read the field comments. Javadoc doesn't even generate the private documentation by default, and IDEs don't show you the field documentation when you use quick help on a method call.
Trejkaz
people don't read the field comments unless they need to. Once there is a need, the more information the better.
akf
+12  A: 

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.

Eric Wendelin
Another valid answer along these lines might be "designs with getters and setters do not properly understand the notion of encapsulation" :)
Trejkaz
A: 

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

Alex B
A: 

Don't put anything if the field name is suficiently descriptive of the contents.

Generally, let the code be self standing, and avoid commenting if at all possible. This may require refactoring.

EDIT: The above refers to getters and setters only. I believe anything non trivial should be properly javadoc'ed.

Thorbjørn Ravn Andersen
There's a difference between commenting and documenting.
Tom Hawtin - tackline
Very true. Exactly therefore is why I do not comment getters and setters. They should be self explanatory, and adding a comment indicates that the code isn't self explanatory.
Thorbjørn Ravn Andersen
+11  A: 

I usually just fill the param part for setters, and the @return part for getters:

/**
 * 
 * @param salary salary to set (in cents)
 */
public void setSalary(float salary);

/*
 * @return current salary (in cents, may be imaginary for weird employees)
 */
public float salary();

That way javadoc checking tools (such as Eclipse's warnings) will come out clean, and there's no duplication.

sleske
Can you fix the typo? "@return part for setters"
Jonik
I think I like this answer best as there is no redundancy and things like cobertura won't make it look like the coder is slacking with his/her commenting
ThaDon
There's also a typo in salary()'s comment. It's not JavaDoc comment.
Fostah
I agree that it is the best approach to commenting accessors.
Fostah
I think the 's' in 'salary' should be in lowercase here, taking into account how these render in the Javadoc output (they are not at the start of a sentence. This Javadoc glitch is a pet peeve of mine.)
Trejkaz
@Trejkaz: You're right, fixed.
sleske
+17  A: 

I'd say only worry about commenting getters and setters if they have some sort of side effect, or require some sort of precondition outside of initialization (i.e.: getting will remove an item from a data structure, or in order to set something you need to have x and y in place first). Otherwise the comments here are pretty redundant.

Edit: In addition, if you do find a lot of side effects are involved in your getter/setter, you might want to change the getter/setter to have a different method name (ie: push and pop for a stack) [Thanks for the comments below]

Gopherkhan
arguably, you should change the name of getters that have side effects to be more clear, as not all developers will read the comments.
akf
That's fine - but that requires users of your API to know that, had there been any side effects, they *would have been documented* !
oxbow_lakes
akf, I was thinking exactly that after posting :) I guess I'll add it to my response.
Gopherkhan
A: 

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?

Henrik Paul
What if getFirstName() returns null? Where would that be documented?
Steve Kuo
How about security.getFinalMaturity()? Not all property names have an immediately understandable meaning. Would you want to be fired for not knowing what that means?
Michael Borgwardt
What if the method is implemented by swizzling? How are you supposed to know that unless it's been clearly documented? How are you supposed to know it is a standard setter unless the doc says it is?
oxbow_lakes
get/set should in my opinion be reserved for getters and setters. Database lookups should be named like "lookupPerson" or so.
Thorbjørn Ravn Andersen
A: 

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

McWafflestix
They're only self-explanatory if you say "this is a property setter". Otherwise a client of the API has no idea whatsoever what is actually happening inside the methods
oxbow_lakes
Who said anything about self-explanatory?
McWafflestix
Sorry - added my comment to the wrong post :-(
oxbow_lakes
A: 

I write the javadoc at the field level to avoid repeating the text in the getter and setter (follow the DRY principle - Don't Repeat Yourself).

/** yearly gross salary */
private float salary;

You may want to reference the field level javadoc from the setter and getter.

/**
 * Setter for {@link #salary}
 */
public void setSalary(float salary);

/**
 * Getter for {@link #salary}
 */
public float salary();
Bruno Rothgiesser
Generated Javadoc does not usually include documentation of private features.
Nat
Then again, most people read the source and don't bother generating docs.
Tom Hawtin - tackline
+1  A: 

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.

oxbow_lakes
Without documentation, any caller would assume that a method called setX sets X. It follows that if setX actually sets X, without doing anything else important, then you don't need documentation.
mquander
That's great! Now does this company CrudTech, whose API I am coding against, follow your convention, or does it follow someone else's on this thread? Hmmmm
oxbow_lakes
There is no point in writing "sets the price" in the setPrice doc if the method just sets the value for the price property, but if it also e.g. updates the totalPrice property and recalculates the tax, such behaviour should obviously be documented.
João Marcus
No; but "JavaBean property setter" is perfectly descriptive
oxbow_lakes
You seem to be asking for the documentation to state "This does what you expect." Which is a bit like writing "Caution: HOT" on a cup of coffee. In a perfect world, there would be no need to ever say such things.
Kevin Panko
Yes - having used APIs where methods called things like `setX` had side-effects other than the expected, I can indeed state with confidence that this is not a perfect world.
oxbow_lakes
Also, it's more like a sign saying **This is coffee** on a cup of coffee. By the way, have you ever seen **2 girls, 1 cup**?
oxbow_lakes
+4  A: 

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:

public void setSalary(float s)
public float getSalary()

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:

/**
 * Returns the height.
 * @return height in meters
 */
public double getHeight()

The first line says it returns the height. The return parameter documents that height is in meters.

Steve Kuo
while I agree with you, I think that one must make sure that the function comments are not making out for a bad chosen, non-explicit function name.
m_oLogin
A: 

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)

Mark Schultheiss
+15  A: 

Absolutely pointless - you're better off without this kind of crap cluttering your code:

/**
 * Sets the foo.
 * 
 * @param foo the foo to set
 */
public void setFoo(float foo);

Very useful, if warranted:

/**
 * Foo is the adjustment factor used in the Bar-calculation. It has a default
 * value depending on the Baz type, but can be adjusted on a per-case base.
 * 
 * @param foo must be greater than 0 and not greater than MAX_FOO.
 */
public void setFoo(float foo);

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.

Michael Borgwardt
+1 I'll hold 'em down, Michael, while you strangle 'em!
Carl Manaster
My sentiments exactly, the worst are the domain specific models where only a domain expert knows what the heck the property means.
ThaDon