102

I often find myself with a dilemma when writing javadoc for properties/members of a "simple" POJO class holding only properties and getters and setters (DTO-style)....

1) Write javadoc for the property
or...
2) Write javadoc for the getter

If I write javadoc for the property, my IDE (Eclipse) will (naturally) not be able to display this when I later access the POJO via code completion. And there is no standard javadoc tag that lets me link the getter-javadoc to the actual property javadoc.

An example:

public class SomeDomainClass {

  /**
   * The name of bla bla bla
   */
  private String name;

  /**
   * @return INSERT SOME SMART JAVADOC TAG LINKING TO name's javadoc
   */
  public String getName() {  
    return name;  
  }  

So, basically it would be interesting to hear how others go about for having your Eclipse IDE display the javadoc property description for your getters - without having to duplicate the javadoc comment.

As of now I'm considering making my practise to only document the getters and not the properties. But it doesn't seem like the best solution...

4 Answers4

80

You can include private members while generating Javadocs (using -private) and then use @link to link to that fields property.

public class SomeDomainClass {
    /**
     * The name of bla bla bla
     */
    private String name;

    /**
     * {@link SomeDomainClass#name}
     */
    public String getName() {
        return name;
    }
}

Alternatively, if you do not want to generate the Javadoc for all private members, you can have a convention to document all getters and use @link on setters.

public class SomeDomainClass {
    private String name;

    /**
     * The name of bla bla bla
     */
    public String getName() {
        return name;
    }

    /**
     * {@link SomeDomainClass#getName}
     */
    public void setName(String name) {
        this.name = name;
    }
}
Chandra Sekar
  • 10,683
  • 3
  • 39
  • 54
  • 2
    I have experimented with both @link and @see tags.. But... at least Eclipse does not display this properly. Eclipse displays the link as a ...(drumroll) .... link.. that one will have to click in order to see the contents. I want to be able to activate code completion (or by mouse over) get the javadoc for a property when I actually are browsing a getter... –  Feb 16 '10 at 14:31
  • 14
    @Kenny - don't model your JavaDoc practices from the POV of Eclipse' usability. Do it from the POV of getting the correct (or sufficiently good-enough) JavaDoc output. IDEs change, and what might be deficient today might be addressed tomorrow (or you might actually change IDEs completely.) – luis.espinal Aug 09 '11 at 19:30
  • 1
    @luis `@link` means a link which has to be clicked to see the actual javadoc. It's not an Eclipse usability issue, it's the wrong solution for providing javadocs that are easy to use. – NateS Aug 19 '16 at 17:12
7

Lombok is a very convenient library for such tasks.

@Getter
@Setter
public class Example {
    /**
     * The account identifier (i.e. phone number, user name or email) to be identified for the account you're
     * requesting the name for
     */
    private String name;
}

That is all you need! The @Getter annotation creates a getter method for each private field and attach the javadoc to it.

PS: The library has many cool features you might want to checkout

Amanuel Nega
  • 1,899
  • 1
  • 24
  • 42
3

I do both, aided by Eclipse's autocomplete.

First, I document the property:

/**
 * The {@link String} instance representing something.
 */
private String someString;

Then, I copy and paste this to the getter:

/**
 * The {@link String} instance representing something.
 */
public String getSomeString() {
    return someString;
}

With eclipse, @return statements have an autocomplete - so, I add the word Gets, lowercase the "t", and copy the sentence with the lowercase "t". I then use @return (with Eclipse autocomplete), paste the sentence, and then uppercase the T in the return. It then looks like this:

/**
 * Gets the {@link String} instance representing something.
 * @return The {@link String} instance representing something.
 */
public String getSomeString() {
    return someString;
}

Finally, I copy that documentation to the setter:

/**
 * Gets the {@link String} instance representing something.
 * @return The {@link String} instance representing something.
 */
public void setSomeString(String someString) {
    this.someString = someString;
}

Then, I modify it and with Eclipse autocomplete you can get not only the @param tag but also the name of the parameter:

/**
 * Sets the {@link String} instance representing something.
 * @param someString The {@link String} instance representing something.
 */
public void setSomeString(String someString) {
    this.someString = someString;
}

Then, I'm done. In my opinion, this templating makes it a lot easier, in the long run, to not only remind yourself what the property means through repetition, but also it makes it easier to add additional comments to the getter and setter if you want to add side effects (such as not allowing null properties, turning strings to uppercase, etc). I investigated making an Eclipse plugin for this purpose but I couldn't find the appropriate extension point for the JDT, so I gave up.

Note that the sentence might not always start with a T - it's just the first letter has to be uncapitalized/recapitalized in pasting.

MetroidFan2002
  • 29,217
  • 16
  • 62
  • 80
  • 26
    Copy/paste is evil... and time consuming. These steps look like a lot of work, and if the javadoc changes you'll have 3 different places to update. I don't think a plugin would justify this either... atleast, then the plugin would have to e.g. consider the property javadoc to be the master and then overwrite getters (and setters). What I want to accomplish is to write the javadoc in 1 single place, and then have both getters and property javadocs assume the same description... –  Feb 16 '10 at 14:27
  • Typically, properties don't change all that often. And the copy and paste operations, with Eclipse's autocomplete, takes less than 30 seconds once the property Javadoc is constructed. – MetroidFan2002 Feb 16 '10 at 14:57
  • 4
    I'm not convinced... Introduction of this type of copy/paste scheme is IMHO bound to lead to inconsistencies. I have too little faith in other cooks (or myself) editing the code later. Also, at least if you're not having a complete up-front design, javadoc properties is often subject to change, at least during a experimental/design phase. And javadoc will be of better quality if written when the code is fresh in mind... Sorry if I seem like a whiner ;-) –  Feb 16 '10 at 15:16
  • 1
    Sorry, but editing *properties* is bound to lead to inconsistencies - either way you play it, Javadoc tends to fall by the wayside unless it is vigorously maintained in some fashion. Even if there was an easy way to expose the property javadoc, it's just as likely that the property javadoc itself won't be updated. It's really a matter of the coding conventions of the team, etc, and code reviews, stuff like that - good luck to you, this is just the way I do it so I don't forget. – MetroidFan2002 Feb 16 '10 at 15:34
  • @Metroid - *unless it is vigorously maintained in some fashion* - well, it is supposed to be vigorously maintained **if** it is treated as part of the source code itself. And not treating Javadoc comments (and their equivalent in other languages) as intrinsic part of code, though sadly being the standard practice, it is the root of many evils. The worst comment is the one that has become out dated. At best, they slow down programmers from getting a grip on code (as they have to constantly revalidate and accept/reject outdated comment.) At worse, they give error-prone, bug-introducing info. – luis.espinal Aug 09 '11 at 19:33
  • @luis.espinal Again, editing *properties* is bound to lead to inconsistencies - if you don't know what the property is supposed to do when you write the code, no one will be able to figure it out later anyways. If you document the property's purpose when you create it, chances are you'll either use it as you meant to, or you'll scrap it in favor of something else, meaning the documentation is usually consistent either way (the property exists as documented, or the property is discarded) – MetroidFan2002 Aug 11 '11 at 13:59
  • @Metroid - editing properties is not bound to lead to inconsistencies. See, if you have to edit a property, say, a nominal change in its name, that mandates a change on any javadoc that makes reference to it (not unlike the update on a method name.) If you discard it, then, just as in the first case, you discard its documentation and you update any javadoc document that references it. This is no different from maintaining documentation on methods, classes and packages. It goes to the heart of what it means *to document*. – luis.espinal Aug 11 '11 at 22:18
  • 'cont - If some piece of code changes or if existing pre/post-conditions, invariants or external system dependencies change, then some other piece of compilable code **AND** code documentation has to change as well. They both make up source code and both have to be equally maintain. The inconsistencies you speak off are not specific to properties, **AND** are the result of not following this principle of code/comment consistency during the evolution of the system, not a result of the nature of properties. – luis.espinal Aug 11 '11 at 22:21
  • @luis - You've just proved my point in a circular fashion - editing properties is bound lead to inconsistencies, because you have to maintain the documentation on anything that uses it. I didn't say it was due to the nature of properties, but the side effects you mention are due to how properties are used. Javadoc tends to fall by the wayside unless it is vigorously maintained, and due to the side-effects of changing a property that you've just documented, changing a property is bound to lead to inconsistencies somewhere along the line unless you have *perfect* documentation. Good luck. – MetroidFan2002 Aug 16 '11 at 15:53
  • Duplicating javadocs is a maintenance nightmare. Are we really arguing this? – NateS Aug 19 '16 at 17:15
-1

I really think it's a problem and the official Javadoc guide does not tell anything about it. C# can solve this in an elegant fashion with the Properties usage (I don't code in C#, but I really think it's a nice feature).

But I have a guess: if you need to explain what someString is, maybe it is a ``bad small'' about your code. It can mean that you should write SomeClass to type someString, so you would explain what is someString in the SomeClass documentation, and just so the javadocs in getter/setter would be not necessary.

  • 1
    About the no proper usage of strings in code, check ``Avoid strings where other types are more appropriate'' in the Effective Java book. – Leonardo Leite Jun 01 '12 at 11:14