Black lives matter
Portrait Dr. Axel Rauschmayer
Dr. Axel Rauschmayer
Homepage | Twitter
Cover of book “JavaScript for impatient programmers”
Book, exercises, quizzes
(free to read online)
Cover of book “Deep JavaScript”
Book (50% free online)
Cover of book “Tackling TypeScript”
Book (first part free online)
Logo of newsletter “ news”
Newsletter (free)

Does Java make you less productive than dynamic languages?

[2010-10-09] dev, java
(Ad, please don’t block)

I still hear from many people that dynamic languages make them more productive, but my experience is exactly the opposite: I used to prefer Python to Java. But then along came Eclipse with its refactoring and code browsing and afterwards, I could not go back to simpler editors. Many of the advanced Eclipse features are possible precisely because Java is not dynamic. But in many ways, Python feels more polished than Java. The irony is that this has nothing to do with Python being dynamic. All of the following things could be easily added to Java.

Standard library. The good news is that Java has libraries for almost anything you can imagine. The bad news is that dynamic languages tend to come with much more useful stuff built in:

  • URL-decoding
  • JSON encoding/decoding
  • UU-encoding
  • Joining strings
  • Command line argument parsing
  • CSV file parsing
  • Complete HTTP client implementation (multipart POST, cookies, ...)
  • Iterators: for loops over iterators, combinations (filter, append, etc.), conversion to collections
  • Collection literals: Java 8 will have collection literals, so we will have to wait a while. In the meantime, Arrays.asList() is good enough for lists and a chainable put() (return type = Map) would be good enough for maps.
  • Triple-quoted strings: If a string is triple-quoted in Python, it can contain multiple lines of text and single quotes.
  • Raw strings: In Python, prefixing a string quote with an 'r' means it is raw and slashes are interpreted as is, and not used for escaping. This is very useful for text that contains backslashes (regular expressions, LaTeX).
Slicing and dicing complex data structures:
  • List comprehension: great in Python, easily added to Java, once it has closures.
  • Accessing the n-th last element (arrays, strings, lists, ...): In Python, the index -n can be used for this purpose. In Java, you have to resort to mylist.get(mylist.size()-n).
  • str() and ref(): Python has two kinds of toString() methods. str() returns a human readable representation, while ref() returns something that can be parsed (i.e. this method serializes to Python source code).
  • Keyword arguments, optional arguments: useful, but might be too cumbersome to add to Java.
Exploring code: Having an interactive command line in Python helps. Eclipse has sheets, but I’ve always found them a bit awkward to use. I am thinking more along the lines of BeanShell, but it does not seem to be updated, any more. I would also love Eclipse to become even better at code browsing (ideas, more ideas).

Most of the other Java warts, I can live with. That Java will have closures sometimes in the future is great, because currently it is difficult to encapsulate a way of iteration, where the operation that is applied to each element can be configured.

Related posts: