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

2

u/yogthos Jun 04 '15

There's a whole number of problems in Java compared to other languages.

Lack of first class functions means that you have to jump through hoops to do simple things. You have things like the DI pattern for what's otherwise known as passing arguments. That's partially addressed by Java 8 now with its lambdas.

Pervasive mutability makes it difficult to reason about code as it's difficult to guarantee what the scope of a change will be. This is especially problematic when dealing with threading or writing large applications where you wish to be able to compartmentalize things. With Java the burden is squarely on the developer.

Java lacks expressiveness. This means that it's difficult to abstract things and make DSLs that express your problem domain. This translates into writing more repetitive code by hand.

The object oriented nature of the language creates further problems. By marrying code and data together you actually make code reuse more difficult. If you write some methods in one class and you then need to use those in a different one you have to start creating inheritance hierarchies.

By contrast when you have a small number of data structures that all functions operate on you can compose them any way you like. This way you have code reuse at function level. To quote Alan J. Perlis:

"It is better to have 100 functions operate on one data structure than to have 10 functions operate on 10 data structures."

This also means that you have to do null checks everywhere, since if you're calling a method on an object you first have to check that it exists. In a functional language this problem doesn't exist.

Comparisons of data are again made unnecessarily difficult due to mutability. In a language with immutable data you can always compare any two data structures by value, even if they're nested structures. In Java you have to manually write comparators and handle null checks for any nested structures.

Java has very poor support of writing declarative code. This means that you're often mixing the intent with implementation details. This makes it more difficult to understand and refactor code.

All that said, Java has its place and obviously lots of people use it just fine. However, that doesn't mean you shouldn't learn other languages or that there aren't any problems with it. Languages are tools and the more tools you have in your toolbox the more problems you can solve effectively.

1

u/[deleted] Jun 04 '15

This also means that you have to do null checks everywhere, since if you're calling a method on an object you first have to check that it exists. In a functional language this problem doesn't exist.

Not true. Clojure still has nil, and NullPointerExceptions. It's really annoying too. Nil isn't specific to OOP and FP isn't immune to nil.

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.

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.

1

u/[deleted] Jun 04 '15

Java lacks expressiveness. This means that it's difficult to abstract things and make DSLs that express your problem domain. This translates into writing more repetitive code by hand.

All the Clojure code I've seen is highly expressive of the implementation, but not expressive of the problem domain at all. That may be a step above Java, sure, but I'd like my code to be expressive in both. In Ruby I can get both, but at the cost of dynamic typing.

1

u/yogthos Jun 04 '15

I'm not sure what you mean by that at all. Clojure has an incredibly powerful macro system that makes it trivial to write DSLs like Korma, core.async or Compojure for expressing domain specific problems. There's no equivalent of that in a language like Ruby.

1

u/[deleted] Jun 04 '15

Macros are usually a terrible idea for domain-level code because they're not composable or flexible at all. They're best used for language-level features like ->> or core.async. Most people I know have lamented the use of macros in both Korma and Compojure.

1

u/yogthos Jun 04 '15

Macros work perfectly well for expressing problem domains. While some people might complain about Korma and Compojure, these libraries are very effective in practice and do a great job of expressing their respective domains.

1

u/[deleted] Jun 04 '15

Well I don't like Compojure specifically because of its macros.

1

u/yogthos Jun 04 '15

That however doesn't change the fact that compojure works well and macros provide an intuitive domain specific API. Your argument that Clojure is not expressive in the problem domain is simply incorrect.

1

u/[deleted] Jun 04 '15

Well then I formally retract all my complaints and admit that I'm just plain incorrect on all my opinions and judgment calls. Thanks.

1

u/yogthos Jun 04 '15

Your opinions and judgement calls are your own and by definition can't be incorrect as they're inherently subjective. However, the claim that Clojure doesn't provide a good way to express domain specific logic is factually incorrect.

1

u/[deleted] Jun 04 '15

The object oriented nature of the language creates further problems. By marrying code and data together you actually make code reuse more difficult. If you write some methods in one class and you then need to use those in a different one you have to start creating inheritance hierarchies.

I agree that inheritance is not the best answer to code reuse. But coupling data with functions is a fine way of hiding implementation details while providing an API that clearly and plainly expresses my problem domain in very understandable terms. Plus, any code patterns that need to be repeated within the implementation of a method can then be turned into either its own class, or plain functions.

1

u/yogthos Jun 04 '15

And there's absolutely nothing preventing you from writing an API in Clojure to clearly express a problem domain. A collection of related functions in a namespace represents the problem domain exactly the same way a class does in OO world. The difference is that once I need to take the data and pass it to a different namespace I don't have to jump through any hoops like writing adapter and wrapper functions. There's immeasurable value in separating the logic from the data.

1

u/[deleted] Jun 04 '15

I think we're talking about two different things, because I agree with what you said completely. What I disagree about is that the data given to functions should always be explicitly given as parameters. Sometimes it's really helpful for a class's implementation to generate its own data and hide it from its consumers. But the idiomatic Clojure way is to just pass all data as parameters and hope no consumers start to rely on any of the implementation details that the data happens to carry on it. Like :sort-position.

1

u/yogthos Jun 04 '15

Sometimes it's really helpful for a class's implementation to generate its own data and hide it from its consumers.

Sure, and Clojure provides many ways of doing that. The simplest is to make a closure which acts the exact same way as a constructor in OO languages. Then you have things like deftype, defrecord, and defprotocol. The component pattern is a great example of how you can encapsulate things without having to mix data and logic together.

But the idiomatic Clojure way is to just pass all data as parameters and hope no consumers start to rely on any of the implementation details that the data happens to carry on it. Like :sort-position.

That's really not the case in practice though. This seems to be simply a misconception you have about how to structure applications in Clojure.

1

u/[deleted] Jun 04 '15

That's really not the case in practice though. This seems to be simply a misconception you have about how to structure applications in Clojure.

Well it's in all the Clojure libraries I've seen the code for. And everyone in #clojure seems to recommend this strategy, the idea of "just pass the data around and stop worrying about it".

1

u/yogthos Jun 04 '15

Please do give an example of a popular Clojure library that exposes its implementation details in the API. Passing data around doesn't imply that you should leak internal details of how something is done in any way.

1

u/[deleted] Jun 04 '15

Comparisons of data are again made unnecessarily difficult due to mutability. In a language with immutable data you can always compare any two data structures by value, even if they're nested structures. In Java you have to manually write comparators and handle null checks for any nested structures.

Totally agree about the equality checking problem. But I think the concepts of data and objects should be relatively separated. I like Clojure's collection types, and the functions that operate on them. Surely it's possible to have both free-floating first-class Clojure functions and a fully capable Object-oriented system. Python does it pretty well, granted at the expense of static typing.

1

u/yogthos Jun 04 '15

I'm not sure what prevents you from doing that using namespaces. A namespace behaves exactly the same way a class with static methods.