I decided to change my question, seeing that a year after that I changed the way I work with zeros:
- I do not use Eclipse builtin null check, because I found it quite primitive (and maybe a little complicated to understand)
- I use
@Nullableto report that the value may be null. In the end, there should be less than zero values than non-zero values. - I am using Java 8, and I prefer to use
Optionalthat allows you to: Optional.ofNullable(value).orElseGet(() -> 1);. It does not break operators ?:and ?.Groovy, but does not necessarily provide a good tool, for example map, filteretc.
And as for my code:
constructors check null values with Objects.requireNonNull, for example:
public Foobar (String a) {this.a = Objects.requireNonNull (a, "a"); }
methods check values using Preconditions.checkNotNullGuava shell whenever I use it in my projects or Objects.requireNonNull:
public void foobar (String a) {Preconditions.checkNotNull (a, "a"); }
The use of one or the other depends on whether I will reuse the value.
I do not check the parameters of the method every time, but more often in the methods public. The idea is not to replace the default validation, which makes it NullPointerExceptionmore efficient than I can.
I am currently using annotation @Nonnulleither @Nullablefor all my parameters, fields, method result (return), but I am wondering what is really better:
- , ? (
@ParameterAreNonnullByDefault ). , ( , , findbugs) com.foobar @ParameterAreNonnullByDefault, com.foobar.example?- ( )
@Nonnull?
, Eclipse 3.8 . "" :
@ParameterAreNonnullByDefault
class Foobar<E extends Throwable> {
@Nullable private Constructor<E> one;
@Nullable private Constructor<E> two;
public Foobar(Constructor<E> one, @Nullable Constructor<E> two) {
this.one = Objects.requireNonNull(one, "one");
this.two = two;
}
public E getInstance(String msg, Throwable t) {
if (null == two) {
return (E)one.newInstance(msg).initCause(t);
}
return two.newInstance(msg, t);
}
}
, ?