Hacker News new | ask | show | jobs
by justin_oaks 760 days ago
You don't?

If that's the case, can you explain why?

2 comments

Null checking can be fine if a failure mode is unambiguous. However, if an operation can fail for many reasons, it can be helpful to carry that information around in an object. For example with URL parsing, it might be nice to be able to know why parsing failed. Was it the lack of protocol? Bad path format? Bad domain format? Bad query format? Bad anchor format? This information could theoretically be passed back using an exception object, but this information is eliminated if null is returned.
Exceptions and null both require the caller to 'deal with it' verbosely, so neither saves lines of code.

But the only thing more annoying than a loud failure (exceptions) is a silent failure (null) which keeps executing.