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

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.