I\'m pretty new to Guava (let\'s be honest, I\'m not \"pretty new\", I\'m a complete rookie on that subject) and so I decided to go through some documentation and got quite
I have read this whole thread few minutes ago. Nevertheless I was confused why should we use checkNotNull
. Then look over Precondition class doc of Guava and I got what I expected. Excessive use of checkNotNull
will degrade performance definitely.
My thought is checkNotNull
method is worth required for data validation which comes from user directly or very end API to user interaction. It shouldn't be used in each and every methods of internal API because using it you can't stop exception rather correct your internal API to avoid Exception.
According to DOC: Link
Using checkNotNull:
public static double sqrt(double value) {
Preconditions.checkArgument(value >= 0.0, "negative value: %s", value);
// calculate the square root
}
Warning about performance
The goal of this class is to improve readability of code, but in some circumstances this may come at a significant performance cost. Remember that parameter values for message construction must all be computed eagerly, and autoboxing and varargs array creation may happen as well, even when the precondition check then succeeds (as it should almost always do in production). In some circumstances these wasted CPU cycles and allocations can add up to a real problem. Performance-sensitive precondition checks can always be converted to the customary form:
if (value < 0.0) {
throw new IllegalArgumentException("negative value: " + value);
}