Hacker News new | ask | show | jobs
by Tichy 5049 days ago
It's just that Java is very verbose, and actually I found it particularly horrible for data driven applications (by this I mean apps whose behavior is determined by data/config files, not "Big Data" - I have no experience with the latter). For complex data types you always need to create complex class hierarchies. In other languages you could just write

webInfo = {url: "bla.bla", title: "bla die blub", links: ["link1", "link2"]}

Notice that webInfo contains two different types, Strings and Arrays. In Java arrays or hashes you can not easily mix types - you'll end up just putting objects everywhere, then be forced to litter the code with type casts. Or you create the unwieldly class hierarchy. That is my prediction, anyway - I am too lazy to come up with a good example :-(

You can also not simply write something like the hash above. The nearest you can get is if you have created that class hierarchy with suitable constructors, you could instantiate that in one go. At least that is my memory - I have now avoided it for so long that I am not even sure how to instantiate an Array or a Hash with data on the fly anymore.

I think instantiating an array with data goes something like

links = new String[]{"bla", "blub"}, and there is nothing like that for Hashes - you are stuck with

info = new HashMap()<String, Object>;//generics are particularly ugly and annoying

info.put("links", new String[]{"bla", "blub"});

info.put("title", "some stupid web site");

info.put("url", "undisclosed");

And so on - a far cry from the example above. (Note the Java syntax is probably wrong, created from memory - but it is something like that).

Even if you went through the mind numbing work of creating appropriate classes, you'd be stuck with

info = new WebInfo(title, url, new String[]{link1, link2,...});

And that is just for two different types, and notice that there is no way to see what the name of the parameters of the WebInfo constructor actually are from that snippet of code.

title: someTitle

is actually much more readable because you can instantly see that someTitle is supposed to be a title.

Also if you want to use NoSQL, I suspect converting java classes to JSON could be a pita, too.

2 comments

link can be a single String? I can see here new "info = new WebInfo(title, url, new String[]{link1, link2,...});" that it can't. This is the kind of thing you get with a typed object. Also, instead of Strings for title, url, etc you could use tiny types. Yes, it is a lot more verbose, but it comes with a advantage (compile time erros over runtime erors). TinyType also documents what you should pass for the constructor. If you need some preparation to get a url (I don't know, like, finding it on some txt), typing would lead you to do it.
If you enjoy that kind of programming, sure, go for it. It certainly is possible to program a crawler in Java. Personally I can't go back since I experienced more succinct languages.

Also beware of pseudo work: I suspect Java is partly popular because it makes you feel productive. You are constantly busy creating Tiny Types (as you call them), generating code in Eclipse (cool: one click and you have 50 lines of code in your class) and so on. It is all just pseudo work that accomplishes nothing, but maybe feeling productive is worth it.

Gson solves that problem by parsing Json to objects or untyped hashmaps. It's OK to mix a mini language into your program.

ImmutableMap.Builder gives you almost syntax-free hash map creation.

You can use constructors in a loop or Lists.transfrom for data-driven construction. Not exactly like JavaScript, but gets close.