-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Reduce unnecessary asserts #239
Comments
Official java guide on assertions: http://docs.oracle.com/javase/8/docs/technotes/guides/language/assert.html In particular, I found this interesting:
which hints that we should not be doing |
In practice, are we able to do proper argument checking in all public methods? It's one thing to check arguments to a really public API such as a component API (e.g. Model, Storage) but argument checking in all public methods in classes inside a component can be messy. So in practice, we could end up not doing any argument checking at all in those classes which is worse than using asserts?. |
I've done some research on this and found out Google Guava's Preconditions The nice thing about Preconditions is that it will throw the necessary exceptions like As an added bonus, public void fooMethod(Foo args) {
return bar(checkNotNull(args))
} This ensures that the |
Related PR oss-generic/process#38 |
Parameter validation is done by using asserts, and throws AssertionError when validation fails. Asserts may not always be enabled. Furthermore, specific exceptions such as NullPointerException should be thrown to make the error more explicit. Let's - Replace the asserts for public APIs with checkNotNull() and checkArgument(), which throws an appropriate exception when validation fails. - Enhance CollectionUtil#isAnyNull(…) to do the same operation as Objects#requireNonNull(T), and also able to take in multiple parameters.
Minor code update
We need to define where to use asserts and where to omit them. For example, asserting every parameter to every method as not null may be unnecessary.
The text was updated successfully, but these errors were encountered: