-
Notifications
You must be signed in to change notification settings - Fork 10.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Precondition methods that always fail for if statements #1164
Comments
Original comment posted by [email protected] on 2012-10-11 at 08:02 PM I forgot to mention stateInvalid would always throw an exception... the return type is analogous to Throwables.propagate(). |
Original comment posted by [email protected] on 2012-10-11 at 08:38 PM How is this different from throw new IllegalStateException(String.format(message, errorMessageArgs)) ? |
Original comment posted by [email protected] on 2012-10-11 at 08:47 PM Consistency and less typing. The formatter in Preconditions.checkState is not the same as String.format. I'm assuming the one in Guava is perhaps faster? Of course one could make the argument for any of the Preconditions.check* "of how is it any different then this inline Java" ? |
Original comment posted by [email protected] on 2012-10-11 at 09:39 PM The Precondition methods are handy one-line versions of things that would otherwise be more cluttered with plain Java, but the "throw new IllegalStateException" approach is still one line -- it's just a few more characters. The formatter in Preconditions.checkState isn't necessarily faster, and doesn't necessarily have to be -- we're not particularly concerned about the speed of throwing exceptions. Preconditions.checkState uses a "subformat" of the format used by String.format -- only accepting %s patterns -- so as to be emulatable in GWT without too much code bloat. That's all. |
Original comment posted by [email protected] on 2013-05-03 at 09:21 PM Merging this into 1382, rather than the other way around, because the discussion seems more involved there. Status: |
Original issue created by [email protected] on 2012-10-11 at 07:59 PM
This may just be me but it seems that it would be nice for Preconditions to support something like:
public static IllegalStateException stateInvalid(String errorMessageTemplate, Object... errorMessageArgs)
The idea would be to support something like:
if ( isABadStateBooleanExpression ) {
throw Preconditions.stateInvalid(...);
}
// else if or just else
else {
return something;
}
The reason I sometimes prefer doing the above as opposed to checkState is to sort of mimic pattern matching like syntax and semantics (ie expression).
I don't terrible need it because I have written my own Precondition class where I can use my own custom exceptions (mainly for debugging purposes and consistency with SL4J's formatting using '{}'): https://gist.github.com/3875102
The text was updated successfully, but these errors were encountered: