Say I have a java method that returns an array of strings. Inside the method, I have more than one return statements, depending on conditions.
public String[] userLogIn() {
String[] success = {"You", "are", "the", "king"};
String[] errorMsg = {"You", "are", "nothing"};
double ran = Math.random();
if(ran < 0.33)
return success;
else if (ran < 0.66)
return errorMsg;
else
return null;
}
This example might be too naive. But my point is, should/can I use multiple @return tags, like
@return this array if condition 1 is met
@return that array if condition 2 is met
@return null if condition 3 is met
?
I often write one single @return the login message
but think this makes less sense when there is a null return.
Which way is a better coding practice in general?
Before using JavaDoc tool, you must include JavaDoc comments /**……………….. */ providing information about classes, methods, and constructors, etc. For creating a good and understandable document API for any java file you must write better comments for every class, method, constructor.
We can use the @see and @link tag multiple times in a class, package, or method. The @see tag declares references that point to an external link, class, or method. The @link tag can also be used multiple times for declaring inline links or in contrast with other block tags.
Javadoc provides the @link inline tag for referencing the members in the Java classes. We can think of the @link tag as similar to the anchor tag in HTML, which is used to link one page to another via hyperlinks. Similar to the anchor tag, the path_to_member is the destination, and the label is the display text.
You can only have a single @return
tag in a Javadoc comment. Javadoc is written for the person using your method. It should describe what the method does, not how it does it. The number of return
statements in the method is compeletely irrelevant to the documentation reader, the only relevant thing is what your method returns for what inputs.
In your case, you could for example document your example method as follows:
/**
* Logs in the user.
*
* @return the result of the operation if the logging in is successful, or
* an error message describing the failure if it is not
*/
public String[] userLogIn() {
...
}
You can only have one @return
tag in valid Javadoc, and the convention as done by parts of the API has been to describe what it returns in that single line.
For your case, you would specify all three conditions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With