Showing posts with label ruby. Show all posts
Showing posts with label ruby. Show all posts

Thursday, August 05, 2010

Java verbosity again

(Repost of a comment I made somewhere on the topic of the verbosity of Java, and particularly its APIs)

The Reflection API is a fairly shocking example of the needless verbosity of Java. And I would consider the official API to be part of Java.

Here's a simplistic example in Java and Ruby, replacing characters in a string ("putty" => "puppy") first directly and then via reflective invocation:

// Java
String s = "putty";
System.out.println(s.replace('t', 'p'));
try {
Class c = s.getClass();
Method m = c.getMethod("replace", char.class, char.class);
System.out.println(m.invoke(s, 't', 'p'));
} catch (Exception e) {
e.printStackTrace();
}

# Ruby
s = "putty"
puts s.gsub("t", "p")
puts s.send("gsub", "t", "p")


This kind of verbosity is prevalent in Java's standard libraries, particularly when (anonymous) inner classes are involved. At the moment I'm struggling with "doPrivileged" blocks in some Java code which deals with sandboxing, and it is syntactically very unpleasant.

Ok, we could chain the Java calls together and knock some lines off, but it's still not nice:
try {
System.out.println(s.getClass().getMethod("replace", char.class, char.class).invoke(s, 't', 'p'));
} catch (Exception e) {
e.printStackTrace();
}

Thursday, July 15, 2010

Blinkenwords - Windows build

Following up on a previous post, a Windows build of Blinkenwords is now available here. I used a very impressive program called OCRA to automatically bundle the Ruby interpreter and required libraries into a single 4 megabyte (not enormous) packed executable.