Core Java

Why I Like The Verbosity of Java

Java is too verbose, they say. You can find comparisons of Hello World programs that take 2 lines in ruby and 10 lines in Java, and in order to read a file you need 20 lines in Java and just 1 in php.

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 :

Bozhidar Bozhanov

Senior Java developer, one of the top stackoverflow users, fluent with Java and Java technology stacks - Spring, JPA, JavaEE, as well as Android, Scala and any framework you throw at him. creator of Computoser - an algorithmic music composer. Worked on telecom projects, e-government and large-scale online recruitment and navigation platforms.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

9 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Manuel Garcia Sanchez
11 years ago

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

Young
11 years ago

In fact,java can also be short and simple.like spring configuration

Sensibility
Sensibility
10 years ago

You could have shortened this whole article to: “Because I’m an idiot and an awful programmer”.

Bozho
10 years ago

Such a mediocre trolling for a programming article; I expected a bit more.

John
10 years ago

lol Java is so fricking verbose it’s hilarious.

capitaine
capitaine
10 years ago

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.

Bozho
10 years ago

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.

Alexian
Alexian
10 years ago

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 »

Meh
Meh
9 years ago

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.

Back to top button