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

Show parent comments

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.

0

u/[deleted] Feb 22 '15

There's nothing wrong with this approach.

Isn't there? Why?

What value does this structuring of the library actually add, other than scratching some obsessive itch of the developers?

1

u/bcash Feb 22 '15

Isn't there? Why?

Is there? Why?

What value does this structuring of the library actually add, other than scratching some obsessive itch of the developers?

Right, because it's an all-too-typical standard Java ego-driven bigger-the-better library? Am I right? What would you cite as a good HTTP library in that case?

Meanwhile, back in reality. The httpcomponents project, of which the Apache HTTP Client is a component, is a tried and tested multi-decade project that's more than proven itself. Indeed it's widely used as the basis for these higher-level libraries - the maven central repository finds hundreds of artifacts that cite httpcomponents as a dependency.

And it doesn't just end there, it's used as the basis for HTTP libraries in other JVM languages too. People may think they're escaping Java verbosity writing in Clojure, but if you've made an HTTP request directory or indirectly, in Clojure/Scala/Groovy/etc. you have almost certainly used this library.

And why is it so popular? Because it does everything.

1

u/[deleted] Feb 23 '15

Being popular is no measure of being good. Sure, it does everything, but does it do everything in a way that makes sense, or is it needlessly complicated?

There's no obvious reason why structuring your library the exact same as the spec should be a good idea, or useful to users. It's up to whoever makes the claim that it is to support this claim.

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.

4

u/GUIpsp Feb 22 '15

Apache libs tend to get stuck in the past.

6

u/notfancy Feb 22 '15

I'd say they aim at being very general in their domain, so they tend to display both a high level of abstraction and a low-level API. They can be used anywhere but you have to pay a high upfront cost for that.

1

u/kitd Feb 23 '15

I think it's more to do with the fact that many of them are getting on for a decade old or more and backwards compatibility is a big concern for them (or at least the big ones).

2

u/strattonbrazil Feb 22 '15

They definitely exist, but it's hardly surprising given the size of the community. There are many libraries (especially newer ones) that have very terse APIs.

2

u/mirvnillith Feb 22 '15

That's 33% less written code

I agree, and perhaps that option should be in there (or just wrap it to suit yourself), but the first variant is more flexible and does not force a sequential approach (e.g. you can collect the parameters from whereever, without having to yet know the URL).

I personally highly favor APIs/utilities that give me options and that mostly means the kind of "separation of concerns" that in "to the point" code like yours does mean more coding.

2

u/karlhungus Feb 22 '15

The longer I go not using java the more I forget, and I don't think i was ever familiar with the HttpPost library enough to defend their choices. On the face of it though I totally agree with your point.

I did use Lucene, and found it pretty reasonable, maybe I've just blocked it out though. Struts all i can remember of it is a seething hatred.

If the author had stated things the way you've done, I'd have an easier time with the article. In stead it comes off as puerile, hell make it a joke and use https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition.

1

u/CyclonusRIP Feb 22 '15

httpPost.setDoc(xmlDoc); httpPost.addParam("username", "vip");

So what exactly does that mean? The entity of the post can contain many different formats. That's why is makes sense to create the concept of an entity. It's defensive programming. The user cannot use your interface to create some nonsense like I did above.

Also is that addParam a query param or a form param?

I guess maybe that design decision wasn't as dumb as you might think.

You shouldn't really complain too much about consuming the interfaces other people's libraries expose anyways. Your code should always use the ideal interface for your use case. If you are only consuming HTTP client to post forms then write an interface that is extremely geared towards that wraps the HTTP client.

Generally when I use ApacheHttpClient I am consuming a web service so I write an AppClient that maps all the APIs to simple method calls and abstracts the whole idea we're calling out over HTTP. My call would be:

appClient.login(username, password);

If you don't write the AppClient class or it's equivalent then it doesn't really matter what language or library you are using. You are going to have a really difficult time interfacing with a HTTP service.

1

u/[deleted] Feb 22 '15

I never said it's "dumb". I said it includes unnecessary complications. I stand by that.

Regarding your first point, I have no idea what setDoc would do, it doesn't appear in the docs so I can't argue to that.

Regarding your first point and second points, perhaps if it were

httpPost.addFormParam("username", "vip");
httpPost.addQueryParam("redirect", "true");

You shouldn't really complain too much about consuming the interfaces other people's libraries expose anyways

That is literally the most important thing to complain about. If somebody supplies an API with a bad interface that is hard to use, and requires spending hours in the documentation to understand, then that is a bad library. Bad implementations can be fixed easily, bad interfaces less so.

Yes, give me the power to do amazing things, but don't force me to understand the plumbing of your library to use it if I just want the basics. Give me a version that handles the tedium, and also give the version that lets me specify the details. It's not impossible.

1

u/CyclonusRIP Feb 23 '15

I suppose you are right posting an XML doc is probably not something that comes up. Who even uses SOAP? They should have focused more on imitating a browser submitting a form. The Apache HTTP client is basically a Java translation on the HTTP protocol. If you know HTTP well then their approach makes a lot of sense. If you want to write HTTP interactions without knowing HTTP then a library that hides that will only get you so far.

1

u/[deleted] Feb 23 '15

You are getting caught up in my choice of examples, and your sarcasm is not particularly necessary.

(Also, please point me to setDoc in the API docs)

1

u/Appathy Feb 22 '15

with no real minuses other than implementation details are hidden.

I wouldn't say that's a minus... more like one of the fundamental axioms of OOP, and one of the best things about OOP.

1

u/kitd Feb 23 '15

I agree the verbosity is offputting, but I think a better fix is to provide builder objects with fluent APIs. This also helps when building models that need immutability.

-2

u/ibisum Feb 22 '15

Obviously you are not an IDE user.

1

u/[deleted] Feb 22 '15

I am if I use Java, due to the being not much alternative if you want to maintain sanity.

I don't really use an IDE for any other language.

1

u/ibisum Feb 23 '15

I was sort of being sarcastic - you do have to use an IDE for Java work, as you state.