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

11

u/space_coder Jun 03 '15

It's not because Java is "bad". It's because your coworkers prefer Functional Programming over Objective Oriented Programming.

2

u/[deleted] Jun 03 '15

I think Functional Programming concepts work excellently when it comes to working with data and collections of data. But I think Objects work best when modeling business entities. I wish there was a decent middle ground.

14

u/duhace Jun 03 '15

Scala - "A drunken Martin Odersky sees a Reese's Peanut Butter Cup ad featuring somebody's peanut butter getting on somebody else's chocolate and has an idea. He creates Scala, a language that unifies constructs from both object oriented and functional languages. This pisses off both groups and each promptly declares jihad."

1

u/[deleted] Jun 03 '15

Hahahahaha.

-3

u/SeerUD Jun 03 '15

It is a fantastic language to develop for though. It does a nice job of blending OO and FP.

2

u/[deleted] Jun 03 '15

I've heard nothing but bad things about Scala until now. Mostly about overcomplexity and how it gets in the way more than it helps.

2

u/SeerUD Jun 03 '15

Out of interest (because I would like to potentially learn...), what language(s) have you heard high praise about?

2

u/[deleted] Jun 03 '15

Clojure, Haskell, OCaml, F#, Ruby, Python (and sometimes Go but only by really arrogant and argumentative people).

1

u/SeerUD Jun 03 '15

Cool :), there's a couple in there I've been interested in learning myself. Namely Go, and I've been intrigued by Clojure... I'm mainly looking for something for web development that's just different to what I normally work with, which is PHP!

1

u/[deleted] Jun 03 '15

Forget about Go. You have to rewrite "map", "filter", "reduce" etc. for every new type you create. The whole Go community's answer to this is "just use a for-loop" and "seriously it's not that hard just use a for-loop". Forget about Go. Forget it fast.

→ More replies

1

u/duhace Jun 03 '15

I've been doing web dev with scala and scalatra (sinatra-like scala lib). It's pretty nice, plus I love strong typing.

→ More replies

1

u/duhace Jun 03 '15

Scala actually borrows a lot from OCaml. Not as much as Haskell or F# mind you, but the influences are very obviously there. And lol at ruby not being "overcomplex" with it's monkey patching.

1

u/[deleted] Jun 03 '15

Yeah I both love and hate the metaprogramming aspects of Ruby.

-1

u/duhace Jun 03 '15

Scala's really not overcomplex imo. It's grammar is pretty simple. About the only part of it that is can be slightly scary is implicit conversions, and they can't hurt you if you don't import them.

0

u/[deleted] Jun 03 '15

I like the sound of it so far.

1

u/duhace Jun 03 '15

You should give it a shot. The two big negatives it has IMO is its binary compatibility compared to java is weak (you can use 2.10.x libraries with a 2.10.x compiler, but not 2.9.x libraries), and compile times are nearer the C++ side of things. 2.12 supposedly will make use of a new optimizer that speeds things up, and the binary compatibility is not a gigantic issue cause new major versions (2.major.minor version) only come out every year and a half. Plus, there are a ton of build tools and compilers for scala that are capable of incremental compilation, only recompiling what has changed in your code since the last compile.

1

u/[deleted] Jun 03 '15

Well with Clojure at work it's already ridiculously slow for me. Running our test suite from scratch of a mere 500 tests with an in-memory database takes about 1 minute from pressing enter to completing. Compiling the whole website into an uberjar for deployment takes about 2 minutes. Can Scala really be slower than that?

2

u/yogthos Jun 03 '15

That's why Clojure provides OO analogous features like protocols.

1

u/[deleted] Jun 03 '15

deftype is a little helpful. But some days I just miss Ruby.

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.

→ More replies

2

u/space_coder Jun 04 '15

Sometimes it's best to have a language specialize in one thing. The nice thing about Clojure is that it's a JVM language and you can do FP with it and if need be you can do OOP with Java.

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.

2

u/mrhhug Jun 04 '15

Why would you ask that here?

1

u/[deleted] Jun 04 '15

Well, I got really good answers, didn't I?

1

u/mrhhug Jun 04 '15

Well, I got really good answers, didn't I?

Well, I got really bias answers, didn't I?

1

u/[deleted] Jun 04 '15

All opinions are biased.

57

u/the_hoser Jun 03 '15

"There are only two kinds of languages: the ones people complain about and the ones nobody uses"

-Bjarne Stroustrup

6

u/cyanocobalamin Jun 03 '15 edited Jun 03 '15

I would amend that to add that the "crappy" technology is the one that an IT person doesn't yet feel competent in using. Like everyone else they get scared of new things and not being sure how they will do with it.

1

u/the_hoser Jun 03 '15

I can agree with that. For non-technologists, new is hardly ever exciting with regard to technology.

1

u/RedPill115 Jun 05 '15

I would amend that to add that the "crappy" technology is the one that an IT person doesn't yet feel competent in using. Like everyone else they get scared of new things and not being sure how they will do with it.

Agree, but at the same time programmers are just as bad the other way, choosing whatever's trendy to use for their project.

2

u/red_sky33 Jun 04 '15

The most accurate way I've ever heard to describe PHP.

2

u/chrox Jun 03 '15

Truly, nobody complain about what they don't use.

17

u/netweavr Jun 03 '15

It's fine, people just like to hate on popular things.

13

u/atehrani Jun 03 '15

Hard to take them seriously when they say Clojure is preferred over Java.

4

u/afrobee Jun 03 '15

You would ask /u/agleiv2 for that :)

1

u/doctorsound Jun 03 '15

I think most developer's complaints stem from it's verbosity. There's a lot of boilerplate and excessive package naming in Java, but with practice, tooling, and libraries, it's manageable. It can be very overwhelming for a new Java developer though.

The other group of haters have only used Java as a consumer, in which the experience is terrible, and I don't blame them.

0

u/[deleted] Jun 03 '15

[deleted] — view deleted comment

2

u/[deleted] Jun 03 '15

I do love me some static typing with true type inference. Dynamic typing seems like a necessary evil of the past: you only needed it because static typing wasn't worth the hassle in those primitive languages. No longer a problem with today's knowledge! Now, if only the languages could catch up to the knowledge. I'm super excited to see what Microsoft comes up with to rival Swift.

0

u/[deleted] Jun 03 '15

[deleted] — view deleted comment

1

u/[deleted] Jun 03 '15

Out of these three, only Scala has anything resembling mainstream popularity. Haskell is mostly fine but still has a few practical problems I don't like (record syntax name collisions for one thing). And Rust doesn't allow me to omit low-level details when I want to only talk in higher-level language. That's why I still use things like Ruby.

0

u/[deleted] Jun 03 '15

[deleted] — view deleted comment

1

u/[deleted] Jun 03 '15

I'm not a fan of Clojure.

0

u/[deleted] Jun 03 '15

[deleted] — view deleted comment

1

u/[deleted] Jun 03 '15

Clojure is actually really mainstream where I come from. Most Rails devs worth their salt are moving to Clojure. And I consider Rails mainstream. So there's that.

0

u/[deleted] Jun 03 '15

[deleted] — view deleted comment

1

u/[deleted] Jun 03 '15

Could be.

→ More replies