4 ms·
Absolutely. So, what I started to do in my java code: Before dereferncing anything for the first time, i add assert foo != null : "foo must not be null - r
by struppi 9y ago
Absolutely. So, what I started to do in my java code:
Before dereferncing anything for the first time, i add
assert foo != null : "foo must not be null - remember to wrap optional data in Optional<T>"
Just make sure to run your program (or at least the tests) with the JVM flag "-ea"
- qznc 9y agoWhat is the point? How is an assert better than a NullPointerException? If asserts are disabled, then the null pointer checks are still inserted by the JVM.
- cpburns2009 9y agoThe benefit of using an assert is that it makes the error condition known immediately to the caller and the reason. Otherwise, whose to say when the NPE will occur. It could be raised just a couple lines down, or it could be a couple methods or a classes away which increases the complexity of diagnosing the actual cause of the error.
- ojhughes 9y agoA more idiomatic approach would be Objects.notNull(foo)