In 1880, Mark Twain wrote a satirical essay entitled “The Awful German Language” where he explains his difficulty in getting the hang of the complicated German grammar. Today, I felt like it was time for me to nag a little bit about the Java programming language.
Don’t get wrong – I’m actually a Java fan. Years ago, I took (and successfully passed) the “Java Certified Developer” exam; I’ve written a considerable amount of Java code in my life. I’ve always loved Java for its run-time environment: the platform-independence you get from the virtual machine and the huge set of powerful libraries. I’m not complaining about Java per se – I’m only complaining about the Java programming language.
Obviously, it’s easy to find weak spots in any programming language – that’s why there are so many of them. Today, I only want to focus on one thing – Java’s wordiness, or rather, Java’s lack of expressiveness.
When developing software, I often start out with a Perl prototype. Yes, Perl is a quirky language, but it is also very powerful and most of its power comes from its very expressive grammar. It is easy to get your job done with Perl.
The other day, I was working on a seemingly simple problem, along these lines: a server produces a text file, containing user vs. usage-time records:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# Format: <user> <time> # Asterisk: secure connection used. Alice 13 John 440 Bob 2 Carl 49 * Peter 90 Alice 219 Bob 8 Alice 72 * Jim 1 Peter 97 Jim 199 |
Another process – my program – takes this list as input, sums up all the usage-times for a user and prints an alphabetically sorted list:
1 2 3 4 5 6 7 8 |
Alice:304 Bob:10 Carl:49 Jim:200 John:440 Peter:187 |
That’s it. Sounds like a five minute job, which it is, if you happen to use Perl:
1 2 3 4 5 6 7 |
#!/usr/bin/perl -w open USERS, "users" or die "Cannot access user list\n"; /^\*?\s*(\w+) (\d+)/ and $users{$1} += $2 while <USERS>; print "$_:$users{$_}\n" foreach (sort keys %users); |
But since I needed this functionality as part of a larger Java product, I bravely recoded it in Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import java.io.*; import java.util.*; import java.util.regex.*; public class GetUsers { public static void main(String[] argv) { Map<String, Integer> map = new HashMap<String, Integer>(); try { BufferedReader input = new BufferedReader(new FileReader("users")); Matcher matcher = Pattern.compile("^\\*?\\s*(\\w+) (\\d+)").matcher(""); for (String line; (line = input.readLine()) != null;) { if (matcher.reset(line).find()) { String key = matcher.group(1); if (!map.containsKey(key)) { map.put(key, 0); } map.put(key, map.get(key) + Integer.parseInt(matcher.group(2))); } } } catch (Exception e) { System.err.println("Cannot access user list"); } for (String key : new TreeSet<String>(map.keySet())) { System.out.println(key + ":" + map.get(key)); } } } |
Wow! I used the regular expression library and the HashMap container class; I even used Java 5’s autoboxing feature as well as generic containers (both of which save you a lot of casting); nevertheless, the Java version is more than four times longer than the Perl script. Here are the stats:
1 2 3 4 5 |
Language: Perl Java Ratio Lines: 5 25 5.0 Chars: 191 834 4.4 |
Is this additional typing really such a big problem? It definitely is! Studies have shown that the number of lines written per developer per time-unit is by and large independent of the programming language used, which means that you are three to four times more productive in Perl than you are in Java. But there is another angle to this problem: if your programming language is highly expressive (like Perl, Python, and Ruby) you are many times more likely to actually write programs. You cannot imagine how many useful programs I have written in Perl that I would have never written in C/C++/Java – just because of all that typing!
I’ve never liked Sun’s “it-is-so-simple-even-a-brain-dead-monkey-can-now-program” paradigm. As a professional software developer – just like a craftsman – I need powerful tools: tools that get the job done in as little time as possible. These tools might be dangerous – very much like a power drill or a circular saw – but in the hands of an expert they can achieve miracles.
But Perl is not perfect, either. In my view, it is good for short programs, less than 1000 lines of code. For larger projects (possibly involving more than one developer) statically typed languages — like Java — are probably a better choice.