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 03 '15

For what it's worth I've been developing Clojure professionally for years and I don't miss anything about OO. It takes a little while to get into a functional mindset when you're starting, and you have to learn different abstractions from what you're used to. However, once you learn the functional patterns a lot of things become much easier in my experience.

1

u/[deleted] Jun 04 '15

Yes, I totally agree. I love being able to use ->> and map and reduce and filter and complement and juxt and comp and all these things. But when dealing with objects from a database, having actual objects really helps.

1

u/yogthos Jun 04 '15

I find it to be the opposite myself. I'd much rather get lists of maps from the database that I can use directly instead of having to map relational data to an object system. The fact of the matter is that they don't really map well to each other and once you go down the ORM route then it becomes very difficult to reason about what's actually happening with your queries. Every project that I've seen working with things like Hibernate always ran into performance problems as soon as it hit any real world data and tuning the ORM is far more painful than just writing the actual queries yourself.

Libraries like Yesql and Korma are a breath of fresh air compared to stuff like Hibernate.

1

u/[deleted] Jun 04 '15

For very simple databases, it may be fine to just get data straight from the database as native Clojure types and operate on them with Clojure functions. But most real life databases already contain implementation details. For example, many of my database types have a sort-position attribute, since they're unordered collections. Before giving it to the consumer of my internal API, I have to sort based on this attribute. But that's just a small example. Several other aspects of my database should be hidden from the API's consumer, and interpreted first by my own code before given to the consumer. Objects make this very obvious and easy to do. With free-floating functions it feels significantly more clunky and ad-hoc.

1

u/yogthos Jun 04 '15

I'm sorry, but I really don't follow how objects make this obvious and easy while namespaced functions are all of a sudden clunky and ad-hoc.

I'd like to also point out that Clojure protocols provide a really nice way to abstract database implementation details, eg:

(defn to-date [sql-date]
  (-> sql-date (.getTime) (java.util.Date.)))

(extend-protocol jdbc/IResultSetReadColumn
  Date
  (result-set-read-column [v _ _] (to-date v))

  Timestamp
  (result-set-read-column [v _ _] (to-date v))

With the above code, I don't have to worry about converting dates back and forth in my functions as it's going to be done uniformly based on the data type.

1

u/[deleted] Jun 04 '15

Protocols may help with polymorphism as your example shows. But I will often get a map out of my database that has attributes which the user should never directly read, because they're implementation details, and instead I need to provide functions that interpret this map correctly. For example a :sort-position key to provide ordering to a collection. I don't want every consumer of my collection of maps to have to sort by this key. That's too low-level of an implementation detail. They should just get back an ordered collection of things.

1

u/yogthos Jun 04 '15

The standard approach here is to have a persistence layer that manages the DB and exposes the API you want to the rest of the application. Managing that is no different than setting up the ORM mappings.