In real applications, you want to tell other programmers what your class and method contracts are. What does the method require from the caller? What does this guarantee its result? Is it thread safe? Avoid this style of documentation:
public void setAge(final int age) {
this.age = age;
}
The documentation contains all the noise; all this speaks of what setAgesets age, and anyone can guess that.
Instead, write this as:
public void setAge(final int age) {
if (age < 0) {
throw new IllegalArgumentException(
"Attempt to set a Person age to a negative value: " + age);
this.age = age;
}
It is also possible to use JSR 303 annotations to document constraints and even enforce them. The signature will be as follows:
public void setAge(final @Min(0) int age);
source
share