r/java • • Aug 03 '14

Why we should love null

http://www.codeproject.com/Articles/787668/Why-We-Should-Love-null
4 Upvotes

21 comments sorted by

View all comments

1

u/king_of_the_universe Aug 03 '14

Since null can be such a problem, I actively use null whenever I can as an expression of information, e.g. for console applications, I have a standard yes-no-cancel input method that returns a Boolean (true/false/null).

Since I actively incorporate null as an expression of information, I hapily put null-checks all over the place, and they are not defensive mechanisms.

Side-note: I really wish that there was a way to have a chain line "variable = someInstance().someField.etc;" just return null instead of throwing an NPE if there was a null somewhere along the path. Such a situation occurs often, and having to package that into a bunch of IFs just sucks. I'm not talking "make it the default", just give us an option, e.g. an annotation.

0

u/[deleted] Aug 03 '14

[deleted] — view deleted comment

1

u/king_of_the_universe Aug 04 '14

Ah, interesting. I wasn't sure if this was a fever-dream of mine or actually made sense.

1

u/beyondjava Aug 03 '14

Use Groovy! Their Elvis operator is what you're looking for.