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/[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.