Why I Like The Verbosity of Java
Even though the examples are often exaggerated (for example counting imports), it is true Java programs requires more lines of code. But this is not a bad thing at all.
On the contrary – it is something I actually like. In fact, it is not about the verbosity of the language – apart from anonymous classes-insteadof-closures, there is nothing else that the language is too verbose about. It is about the core libraries. So – I like the way the core libraries are written in terms of verbosity. Two examples:
- take the java.io. package. Reading and writing files, streams, etc. It is a bit hard to graps, and in the beginning you copy-paste long snippets of code to simply read a file. But it forces you to understand the abstraction of streams and readers. Other languages have simply:
var contents = readFile("path")
Cool, but you are never forced to understand how the I/O management works. What happens if reading fails? Is partial reading of the file sufficient for you? Can you nagivate the file? Should you close resources or they are automatically closed? You don’t need to answer these questions for a hello world program, but you will need to know about them pretty soon. And the less-verbose languages hide them from you and postpone this “abstraction revelation”. - the servlet API. At first it looks to have some hairy classes and interfaces. But soon enough you realize how the whole thing works – not only in Java, but the general lifecycle of an http request. Because you need a Servlet object, and request and response objects, and output streams to write to, you understand the whole request-response cycle. I have a personal example here. I’ve been writing PHP for one year (in school). Then one month of Java and servlets made it completely clear for me how the whole thing works. PHP was very easy to use –
$_GET['foo'], session_start()
and a bunch of HTML in between. So I didn’t bother to understand the underlying mechanics. Java forced me to.
You may argue that – fine, it forces you to learn these important concepts and abstractions, but it should also give you an easy way to acomplish things. But if the core libraries themselves had these options, all the tutorials would show these options, and the lower-level APIs would be forgotten.
So the solution is – 3rd party libraries. Apache and Google give you these. With guava and apache commons you have all these one-liners. FileUtils.readLines(..)
, Joiner.on(",").join(array)
, etc. But you don’t start with these libraries, and you learn how things function on a slightly lower level – a level that you will be required to know anyway.
Reference: Why I Like The Verbosity of Java from our JCG partner Bozhidar Bozhanov at the Bozho’s tech blog
Related Articles :
Agree! Clear and explicit code is much better than simple or short code for maintenance or refactoring. Many coders simply don’t understand that fact
In fact,java can also be short and simple.like spring configuration
You could have shortened this whole article to: “Because I’m an idiot and an awful programmer”.
Such a mediocre trolling for a programming article; I expected a bit more.
lol Java is so fricking verbose it’s hilarious.
I think the best thing to do is to switch to Ruby or Clojure for a while. Then come back to Java and see if you feel some heaviness.
Well, in fact I’ve been extensively using Scala for the past 7 months, and I still stand behind every word in the article. It doesn’t feel heavier, especially with the lambdas in Java 8.
Well I agree with some parts and some others not. I mean I do like some verbosity of Java, since it’s self explanatory sometimes and there ain’t any need to struggle with comments in order to explain what you are doing, but what about getters and setters? and what about anonymous classes for listeners? I have to admit that I much prefer Java8 than any previous version of it and I started to study Scala for let me “write less to tell more”. However where I live Scala is not an “acceptable” language, unless you’re building something totally new for… Read more »
High level programmers are not serious programmers, if you think Java to be too verbose I wonder what these same folks working with C/C++ would reason. If the entire argument is about web programming I retract my input.