Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing Javadoc for return-type 'Optional<T>'

I'm currently writing a Java-API for SOAP-webservices used at my workplace.
The webservice-classes are generated with Axis2 and they can return null. As I don't want to deal with null-references on my business logic level, I'm using Optional<> as return-type.
For example:

/**
 * Reads account-data to given accountId. 
 * 
 * @param accountId
 *          the primary key of table 'account'
 * @return  the account wrapped as an Optional<>, if an account with primary key 'accountId' exists; Optional.empty(), else
 */
public Optional<Account> readAccount(long accountId) throws RemoteException, ServiceFaultException {
        // prepare SOAP-Request
        ReadAccount request = new ReadAccount();
        request.setAccountId(accountId);

        // execute SOAP-Request
        ReadAccountResponse response = accountService.readAccount(request);

        // process Response
        Optional<Account> account = Optional.ofNullable(response.getAccount());

        return account;
    }

The above method executes a webservice-operation to search for some account-record in a database. If there is no account to be found with matching argument accountId, the method call response.getAccount() can return null.

Is there a more concise way of writing the Javadoc for @return?
Especially for the phrase "wrapped as an Optional<>"?

(I know that the answers might be opinion based, but I haven't found any recommendations for this on stackoverflow or by googling.)

like image 275
William Avatar asked Jul 05 '19 08:07

William


People also ask

How can I return optional value in Java?

Optional<String> value = Optional. of(str[ 2 ]); // It returns value of an Optional.

What does @SEE mean in Javadoc?

In short, we use the @see tag when we want a link or a text entry that points to a reference. This tag adds a “See Also” heading to the reference. A document comment can hold any number of @see tags, all of which can be grouped under the same heading.

How do you write Javadoc?

Use the standard style for the Javadoc commentJavadoc only requires a '/**' at the start and a '*/' at the end. In addition to this, use a single star on each additional line: /** * Standard comment. */ public ... /** Compressed comment.

How do you write a Javadoc style comment?

Writing Javadoc Comments In general, Javadoc comments are any multi-line comments (" /** ... */ ") that are placed before class, field, or method declarations. They must begin with a slash and two stars, and they can include special tags to describe characteristics like method parameters or return values.


2 Answers

Why not say it how the jdk does? For example Stream::reduce:

@return an {@link Optional} describing the result of the reduction

In your case it would be:

an Optional describing the account.

like image 192
Eugene Avatar answered Sep 29 '22 01:09

Eugene


I would suggest simplying your return statement javadoc to the following:

/**
 * Reads account-data to given accountId. 
 * 
 * @param accountId
 *          the primary key of table 'account'
 * @return the account wrapped in an {@link Optional}
 */
public Optional<Account> readAccount(long accountId) throws RemoteException, ServiceFaultException {
  // function here
}

the reason for this is because Optional.empty() is an expected and unchanging part of the Optional API; every developer who knows what an Optional is will know to expect an empty Optional if the account is missing; and will understand he needs to access the actual information inside the Optional if the account is present.

We provide a @link here to allow developers who haven't heard of Optionals to look up its respective javadoc and understand how it works; this is itself not mandatory, but can be helpful if there are a lot of less experienced developers working on your project.

like image 36
Austin Schäfer Avatar answered Sep 29 '22 00:09

Austin Schäfer