Morad Morad - 4 months ago 13
Java Question

What is the purpose of Guava's Verify?

What is the purpose of

when we have
com.google.common.base.Preconditions
?

The
Verify
class looks nice but it has an
@Beta
annotation, should I use it?

Answer

The difference is semantic. Verify is used to ensure that invariants don't change, that Code which has been engineered to do a certain thing is actually doing that thing. In spirit:

int x = divide(10, 5);
Verify.verify(x == 2, "X should be 2");

Preconditions, on the other hand, are expected to fail when bad input is passed to a certain portion of the program, usually either from the user to the code, or from client code to code within another library. In spirit:

public int divide(int x, int y) {
  Preconditions.checkArgument(y != 0, "Can't divide by 0!");
  return x / y;
}

As to whether you should use a @Beta class, that entirely depends on the scope and foreseeable lifetime of the application you are building, and, asked alone, would probably be flagged as a "Primarily Opinion-Based" question.