Hacker News new | ask | show | jobs
by saagarjha 2557 days ago
I was reading through one of the linked articles of why the author doesn't like using null or static objects:

https://www.yegor256.com/2014/05/13/why-null-is-bad.html

https://www.yegor256.com/2014/05/05/oop-alternative-to-utili...

Apparently he replaces null with null objects or throwing exceptions, and creates objects for every static method. Go figure.

Actually, there are a few gems:

> The Map interface (no offense to its authors) has a design flaw.

> It is a good practice to make your code as fragile as possible, letting it break when necessary.

> The method is basically asking the object about its… race. Black objects go right while white objects go left. That’s what this instanceof is doing, and that’s what discrimination is all about.

1 comments

> Apparently he replaces null with null objects

That's what Optional was introduced for in Java 8. If you want to be absolutely null-safe, using it makes sense. But if you want to optimise performance, going with the slightly more risky route is still the better choice.

That's not really "why" it was introduced. Optional is used as a null wrapper in other JVM languages so that idiom bled over pretty naturally.

Here's a talk "Optional - Mother of all Bikesheds" by Stuart Marks, the guy who added it to OpenJDK: https://m.youtube.com/watch?v=Ej0sss6cq14

It was mostly just meant to cover the zero or one case on fluent-style interfaces like the Streams API, so check as getFirst() or getAny(). In hindsight, the name Optional was a poor choice if they didn't want the idiom coming over.

Passing optional objects as method params is bad. The trouble is that an Optional<T> can still be null itself.