Prefer Guava’s Preconditions for checking
whether a constructor or method was invoked correctly (that is, whether its
preconditions were met). It is more concise than an explicit if-throw
block and clearly communicates the intent of the check.
Preconditions?if-throw block can
often be done in a single line.checkArgument (for
IllegalArgumentException) and checkState (for IllegalStateException)
explicitly state what kind of validation is being performed.checkNotNull returns the validated object, making it
convenient for assignment or usage in a constructor (e.g., this.foo =
checkNotNull(foo);)Preconditions support simple %s formatting
placeholders, avoiding the verbosity of String.format and the performance
pitfalls of manual string concatenation for simple failures.checkNotNull// Before
if (arg == null) {
throw new NullPointerException("arg must not be null");
}
// After
checkNotNull(arg, "arg must not be null");
checkArgument// Before
if (index < 0) {
throw new IllegalArgumentException("index must be non-negative: " + index);
}
// After
checkArgument(index >= 0, "index must be non-negative: %s", index);
This check is conservative and will not suggest a refactoring if:
if statement contains comments that might be lost during refactoring.if statement has an else block or is part of an if-else-if chain.&& or ||) that would
require a complex/unreadable negation.Preconditions method (e.g., inside checkNotNull itself) to avoid
infinite recursion.Suppress false positives by adding the suppression annotation @SuppressWarnings("PreferPreconditions") to the enclosing element.