Java as a Scripting Language?
Join the DZone community and get the full member experience.
Join For FreeI came across a language comparison (which I wish I could still find) where the author presented a code sample in many different languages. The example he chose was computing the MD5 digest of a string. He showed a verbose Java version, some python etc. Finally php:
md5("Hello World");
This example, the author asserted, showed that PHP coders can do as much with one line as a Java coder can do in 20. Of course any problem is easy in any language if there’s an available library call that’s just right.
When I have to write a program to work with lines of text I’ll usually turn to Ruby or possibly Groovy (sorry Perl – not going there anymore). Scripting languages like these are usually geared toward text processing and their built in libraries make these jobs easy. I wouldn’t jump into Java because it’s such a pain to do this kind of thing.
Ruby
File.open("myfile.txt").each { |line|
puts line if line =~ /blue/
}
some standard Java to do the same thing
import java.io.*;
public class ProcessWords {
public static void main(String [] args) throws IOException {
BufferedReader input = new BufferedReader(
new FileReader("myfile.txt"));
String line;
while ((line = input.readLine()) != null)
if (line.indexOf("blue") != -1)
System.out.println(line);
input.close();
}
}
The Java code is obviously more verbose and uglier, matching many people’s opinion of Java in general. But is this because of the language or the API? Java’s APIs are all very general and give you complete control over everything you do. Ruby’s APIs make it easy to perform this common task.
You could easily create a TextFile
class in Java with a linesMatching
method that returned Iterable<String>
allowing you to iterate over lines that matched a regular expression. Now the task is easy:
public class ProcessWords {
public static void main(String [] args) throws IOException {
for (String line : new TextFile("myfile.txt").linesMatching("blue")) {
System.out.println(line);
}
}
}
The designers of Ant decided programmers like writing in XML. But I don’t. I’d rather write in Java than XML. Would ant build scripts be better expressed in Java?
My hypothetical translation into Java.
public class Ant implements ProjectDefinition {
void targets(Project project) {
project
.add(new Target("clean") {
void execute(Tasks tasks) {
tasks.delete("build");
}
})
.add(new Target("compile") {
void execute(Tasks tasks) {
tasks.mkdir("build/classes");
tasks.javac("src").destdir("build/classes");
}
});
}
}
Both definitions are roughly the same size. The hypothetical Java version definitely has some cruft but is reasonably compact. Like Rake though, the Java version would allow you to use the power of a real language in your build script – conditionals loops, dynamic targets, variables. A Java version would give you instant IDE support, debugging, profiling on top of that.
Both the line iteration and ant project definition examples show internal domain specific languages. The first domain is text processing. Scripting languages compete in this space, but I would argue it has a lot more to do with the libraries they provide than the language syntax. The second domain is build configurations. With the right APIs Java would do a very good job here too.
Of course Java does have several strikes against it when you actually consider its use for scripting:
- you have to compile all the files which turns some people off. You could easily write a ClassLoader to compile the code on the fly.
- Java starts up slowly, which kills the performance of very quick scripts, if that matters.
- Java has no meta programming. This is probably the biggest issue, although reflection and code generation can help here. (You could generate the ant task APIs for my build script example).
I think more user friendly APIs could be written for Java for areas like text processing, XML parsing and creation, threading (even easier than java.util.concurrent), and file operations. Joda Time is a great example of a library that is cleary superior to Java’s in this respect.
When I write an API (in any language) I try to think of what would make the user happiest when coding against it, not what necessarily matches the implementation. Chained method calls for example don’t help at all in the implementation of a class, but returning this
from each method can help the callers in some cases and is easy enough to justify. In some cases providing a little internal DSL instead of a collection of getters/setters and unrelated methods makes the code a lot more readable and helps keep the focus on the caller instead of the implementation.
From http://dmy999.com/
Opinions expressed by DZone contributors are their own.
Trending
-
Five Java Books Beginners and Professionals Should Read
-
TDD vs. BDD: Choosing The Suitable Framework
-
Front-End: Cache Strategies You Should Know
-
How to Use an Anti-Corruption Layer Pattern for Improved Microservices Communication
Comments