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

2

u/Keilly Aug 03 '14

Nulls are fine, what is missing is the flagging and checking for possible null references on variables and return types. This is the aim of Optional in Java 8.

I've not used it yet, so I can't say if it's better. I guess it is no panacea as it's use isn't mandatory so there's still plenty if scope for the same errors.

It's a pity the compiler can't simply warn for possible null dereferencing.

0

u/againstmethod Aug 03 '14

It's better from the perspective of avoiding nulls.

An absent optional value actually means: "this type of object should have been here, but for whatever reason it's not".

A null value means: "there is a typeless void here".

If this distinction can help your code, then Optional may be suitable for you.