Byzantine Reality

Searching for Byzantine failures in the world around us

Simplicity, Part 1

Just a random little mumbling for today: I was on the StackOverflow message forum and saw a questionasking how to find your gateway’s IP in Java. The easy way is to do it via a shell command, but in Java this got brutally complicated, and while I was doing this, all I could think was “geez, how much simpler is this in Ruby?”

Here’s why the Ruby guys are all so happy (and why I’m frustrated at Java). Here’s the Java code to do it:

import java.io.*;
import java.util.*;

public class ExecTest {
   
public static void main(String[] args) throws IOException {
       
Process result = Runtime.getRuntime().exec("traceroute -m 1 www.amazon.com");

       
BufferedReader output = new BufferedReader(new InputStreamReader(result.getInputStream()));
       
String thisLine = output.readLine();
       
StringTokenizer st = new StringTokenizer(thisLine);
        st
.nextToken();
       
String gateway = st.nextToken();
       
System.out.printf("The gateway is %s\n", gateway);
   
}
}

And here’s the Ruby code:

result = `traceroute -m 1 www.amazon.com`
result
.each do |line|
 
if line =~ /\d  (\d*.\d*.\d*.\d*)/
    puts
"The gateway is #$1"
 
end
end

Holy crap that’s a huge distance. And for those of you more into the numbers, here’s what word count has to say:

     lines   words chars
       
6      21     141 ExecRuby.rb
     
14      50     502 ExecTest.java

This is a huge difference, but since numbers can be misleading, I’d rather measure the time it took me to wrote these: it took about 5 minutes to write the Java version, and half that to write the Ruby version. I’ve known Java for a while but since I’m a little rusty, I got hung up a bit on the little syntax stuff and dumb library crap. However, I don’t really know Ruby that well and am still learning it, and I still got it done in half the time!

A lot of this owes to Ruby being much simpler on the syntax. Many argue it’s the dynamic / duck typing, but C# 3.0 is evidence against that with their new local type inference feature. It’s just easier to get it done in Ruby (at least in this example), and I’m suspecting this so much that the title of this post is numbered for possible future cases of this.