It often happens that a method imposes restrictions on its arguments, which cannot be described by a type system. For example, a method may require that any argument be non-empty, or some input argument be positive. There may also be more complex preconditions, for example, that a certain method was previously called or that a certain object is in some state. What is the best way to document this in Javadok?
For example, suppose I have the following public library function, where the argument cannot be negative:
public void foo(int bar) { if (bar < 0) { throw new IllegalArgumentException("Negative bars cannot be food."); } ... }
I want to document this in such a way that it "stands out" from the rest of the documentation document, so that readers of the documentation immediately know where they should look. I am currently doing this by adding throws sentences to Javadoc:
public void foo(int bar) { ...
However, this introduces an exception throw into the specification of the method. Library users can now depend on this behavior in their code. Therefore, if I want to change the method in the next version in such a way as to allow negative parameters, I could break the client code.
Are there any recommendations for documenting such things in Javadoc? I thought:
- Describing in the documentation that the method has undefined behavior if the argument is negative. However, this does not particularly stand out, so many library users may miss it.
- Using annotations (
public void foo(@NonNegative int bar) ). However, for this a standard set of annotations would be ideal, and this standard set does not exist.
source share