yf0994 / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Precondition methods that always fail for if statements #1164

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
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

Original issue reported on code.google.com by adam.g...@evocatus.com on 11 Oct 2012 at 7:59

GoogleCodeExporter commented 9 years ago
I forgot to mention stateInvalid would always throw an exception... the return 
type is analogous to Throwables.propagate().

Original comment by adam.g...@evocatus.com on 11 Oct 2012 at 8:02

GoogleCodeExporter commented 9 years ago
How is this different from

throw new IllegalStateException(String.format(message, errorMessageArgs))

?

Original comment by lowas...@google.com on 11 Oct 2012 at 8:38

GoogleCodeExporter commented 9 years ago
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 by adam.g...@evocatus.com on 11 Oct 2012 at 8:47

GoogleCodeExporter commented 9 years ago
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 by lowas...@google.com on 11 Oct 2012 at 9:39

GoogleCodeExporter commented 9 years ago
Merging this into 1382, rather than the other way around, because the 
discussion seems more involved there.

Original comment by lowas...@google.com on 3 May 2013 at 9:21

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:13

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:08