r/programming • • Feb 22 '15

Java Makes Programmers Want To Do Absolutely Anything Else With Their Time

http://www.forbes.com/sites/quora/2015/02/21/java-makes-programmers-want-to-do-absolutely-anything-else-with-their-time/
35 Upvotes

636 comments sorted by

View all comments

140

u/karlhungus Feb 22 '15

I have no love for java.

While it performs pretty well nowadays, it still isn’t competitive with C or C++ and, with a little love, Haskell and OCaml can or will eclipse it in that domain.

I didn't think this was true, i thought it was somewhat close to C (excluding startup).

For real-world production servers, it tends to require a fair amount of JVM tuning.

This is really seems like reaching for something to be a problem that isn't.

because it objectively sucks.

If people can't even define OO, it can't objectively suck. If you were making a pun i forgive you.

culture is one of mediocrity and bad taste, with MetaModelVibratorVisitorFactory

These kinds of strawmen make me feel like "I don't have to read another word this person writes."

61

u/[deleted] Feb 22 '15

These kinds of strawmen make me feel like "I don't have to read another word this person writes."

I agree to an extent - but I do think the Java community overembraces creation of single-purpose objects over just calling functions.

See this example from the Apache HTTP client docs:

HttpPost httpPost = new HttpPost("http://targethost/login");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("username", "vip"));
nvps.add(new BasicNameValuePair("password", "secret"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
CloseableHttpResponse response2 = httpclient.execute(httpPost);

First off, it's great that they used an ArrayList<> instead of a NameValueCollection() or something, but wouldn't the code be better like:

HttpPost httpPost = httpclient.post("http://targethost/login");
httpPost.addParam("username", "vip");
httpPost.addParam("password", "secret");
HttpResponse response = httpPost.execute();

That's 33% less written code, with no real minuses other than implementation details are hidden.

It seems very endemic to Java libraries that the API is written to require the full power of the library, rather than being written to be simple to use. In this case, it's written to allow a lot of flexibility in how forms are encoded, in how name-value pairs are constructed, etc., at the cost of needing to build (unnecessary) HttpPost, NameValuePair and UrlEncodedFormEntity objects.

See also: Swing, Apache Lucene, Struts, many other popular frameworks.

28

u/pfitzsimmons Feb 22 '15 edited Feb 22 '15

Some newer libraries are getting better at this. For example, now you can use Unirest for http in java, which is even simpler than your example:

Unirest.post("http://httpbin.org/post")
  .queryString("name", "Mark")
  .field("last", "Polo")
  .asJson()

Also, the new Java RESTful web services API is actually pretty good. I actually like its way of defining endpoints better than how python frameworks like django and flask do it.

But yeah, it really does baffle me how so many java libraries seem to go out of their way to make you use five lines instead of one. Why not add a bunch of helper methods for the most common use-cases? Time adds up when everything you do is twice as verbose as it needs to be.

18

u/bcash Feb 22 '15

There are, as other comments have mentioned "better" HTTP libraries that present a much higher-level API. But Apache's HTTP Client has the specific aim of implementing HTTP as it's defined, and designed it's class-structure with that as a goal, allowing the various options to be substituted.

So, of course, form submission will be the most verbose example, as URL-encoding pairs of values is a convention, the mechanics of POST go far beyond that one case (yes, that one case is the most common).

There's nothing wrong with this approach. Indeed it's very valuable for non-trivial circumstances. But if you don't need it, there are other libraries available.

→ More replies (3)

15

u/LOOKITSADAM Feb 22 '15

It seems very endemic to Java libraries that the API is written to require the full power of the library, rather than being written to be simple to use.

I seem to be in the minority, but I absolutely love this aspect of big java libraries. There's always something to be done about just about everything. If I want to change how expressions are parsed in a .jsp document, I can.

11

u/bcash Feb 22 '15

Exactly. Most language comparisons around here come down to "here's an example I don't like, therefore it's wrong" often accompanied by a citation to some completely different tool.

There are trade-offs. If you need to control the corner cases of HTTP than Apache's HTTP client is a good choice; if you're building a REST API client there are REST clients which do a much better job. One is not right and one is not wrong, they're just serving different purposes.

7

u/gingenhagen Feb 22 '15

That point of Apache HTTP is to create one layer of abstraction above the java.net package. Now, for your application, you can easily create another layer of abstraction and make it look like your sample code. But isn't it so much nicer getting to build using the org.apache.http package instead of having to use java.net?

3

u/tieTYT Feb 22 '15

FWIW I program in java all day and IMHO that HTTP Client has a really inconvenient API. I completely agree with you.

Us Java devs are in a tough spot. Even if it's the best fit, using a "function" in java (a static method) could really screw over the next guy who wants to test the code because it's extremely difficult to replace static things with test doubles.

→ More replies (15)

26

u/unruly_mattress Feb 22 '15

MetaModelVibratorVisitorFactory

These kinds of strawmen make me feel like "I don't have to read another word this person writes."

How is that a strawman? My current project has a class EntityManagerProvider that creates an EntityManager through an EntityManagerFactory, and another one named EntityManagerProviderProvider that provides the previous. This is not even a joke. EntityManagerFactory is standard Java, around 10% of the Java classes I know are named Handler and it's never not been like it whenever I was reading Java code.

11

u/[deleted] Feb 22 '15

I must be on the other side of the Java planet

15

u/[deleted] Feb 22 '15 edited Feb 22 '15

Classes ending in 'er' are code smell IMHO. 'Beer' being a notable exception.

5

u/Commander_Crichton Feb 22 '15

and *Controller

5

u/[deleted] Feb 22 '15

Good point, let's call it LocusOfControl ;)

→ More replies (1)

0

u/[deleted] Feb 22 '15

[deleted]

22

u/[deleted] Feb 22 '15

It's definitely no C, but it's a hell of a lot closer than most of the languages that go into prod these days (excluding the MLs, which despite reddit's hope, get very little industry love).

13

u/slavik262 Feb 22 '15

Yep. Cache misses are a huge and "hidden" cost on modern CPU architectures. Everything being a heap-allocated object in Java means that unless you're iterating over an array of primitives, you're going to be doing a lot of bouncing around in memory. See: http://gameprogrammingpatterns.com/data-locality.html

→ More replies (8)
→ More replies (13)

6

u/CyclonusRIP Feb 22 '15

His description of maven is what really lost me. He doesn't even understand that maven caches dependencies locally.

→ More replies (1)

5

u/[deleted] Feb 22 '15

What about Scala?

8

u/smithywill Feb 22 '15

I like Scala. It does away with a lot of the boiler plate code from Java. Although it does have a steep learning curve, the functional side of it teaches Java devs to be better programmers, it has for me anyway.

13

u/[deleted] Feb 22 '15

I'm glad you mentioned! It is a really powerful language! I think everybody should try Scala and its community to feel the higher-level easy development and break the slave chains of big companies.

6

u/superPwnzorMegaMan Feb 22 '15

It has quite the learning curve though. I thought I'd just start with it and see where it takes me similar to my groovy experience, but it took me quite a whilst longer to create functionality.

I really like the type inference and the immutability, so I'll stick with it for bigger projects in favor of java.

What I did not like is that scala is currently impossible to mix with groovy, it can be mixed with java, but not groovy (at least not in the IDE). You can however let your scala classes handle interpreted groovy scripts, which is pretty cool. So I don't think it should be that hard to fix this problem, I might take a look at it when I have time.

→ More replies (7)

12

u/[deleted] Feb 22 '15

Scala is a horrible language. It's far too flexible. The creators see c++ and thought it didn't give you enough ways to write confusing code so they added even more options. Everything in scala has like 5 ways you can call it and everyone does it a different way making using third party libraries a pain if you ever have to debug.

→ More replies (14)

8

u/DonHopkins Feb 22 '15

These kinds of classnames make me feel like "I don't have to read another line of code this person writes."

6

u/tetroxid Feb 22 '15

You'll love this

12

u/eff_why_eye Feb 22 '15

These kinds of strawmen make me feel like "I don't have to read another word this person writes."

I had the exact same thought and stopped reading the article right at that point. Next I suppose he goes on to complain about stuff in Java 1.2...

→ More replies (74)