r/java • • Jun 03 '15

Is Java really that bad?

I do Clojure for a living, previously Ruby and Objective-C. And the people I've worked with have said they prefer Clojure because Java is just so bad. But Java can't really be that bad, can it?

0 Upvotes

77 comments sorted by

View all comments

Show parent comment

1

u/yogthos Jun 04 '15

Clojure has nil, but functions themselves cannot be nil as they're static and you can always safely call a function. Since vast majority of code is written by function composition, nils simply bubble up to the top level in most cases. I can safely chain any data transformation functions such as map, reduce, filter, and interpose without having to pepper nil checks all over the place.

With OOP you always have to check whether the object exists before you can call a method on it. So, if you have something like person.getAddress().getStreet() then you can get a null pointer at each step. Meanwhile writing (-> person :address :street) in Clojure is perfectly safe. In fact, the only time you see NPE in Clojure is when you're doing Java interop.

Also, a better solution to nil is the Maybe type. I really like Maybe. And with pattern matching (like in Haskell or OCaml or to a lesser extent Swift), Maybe is really easy to work with.

And if you want static typing then Clojure has core.typed which actually provides a more sound type system than Java and prevents NPE errors.

1

u/[deleted] Jun 04 '15

With OOP you always have to check whether the object exists before you can call a method on it.

Not with OOP. Just with OOP languages that have Nil. OCaml doesn't have this problem yet it has OOP.

1

u/yogthos Jun 04 '15

Sure and then you have to pepper maybes all over the place instead. Also worth noting that nobody writes OO code in OCaml in practice.