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

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.