8 ms·
Trivially little compared to the code base and compared to the advantages. It takes only a few seconds to type if (object == null) { throw new IllegalA
by wlievens 17y ago
Trivially little compared to the code base and compared to the advantages. It takes only a few seconds to type
if (object == null)
{
throw new IllegalArgumentException("Parameter 'object' should not be null.");
}
Whereas I could lose hours chasing after null values that were passed through layers of unguarded abstractions.
Sure, I'd love for DBC to be a language feature (Eiffel has it) but exceptions help me out here, too.
- dkersten 17y agoI get that and thats how I handle it too, however, you now have an IllegalArgumentException to handle for the null pointer when IMHO having null pointers should be something out of the ordinary and therefore not possible in MOST of the code.
- wlievens 17y agoThings that are not possible should be asserted to be so. Leaving an assumption in the mind of the programmer only is dangerous and almost worthless. Any assumption reasonably expressible in code should be expressed in code. This goes beyond null checks, it counts for any state. For instance, in a tool that uses graphs as data structures, some transformations might impose that your graph at input and output is acyclic. Don't put this in comments, assert it in code! The exception is an exception in the strictest sense: something that should not happen. I consider IllegalArgumentException to be an exception you should never catch (except in unit tests, perhaps), but instead fix the root cause. Unlike IOException for instance.