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.

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)