Languages Part II

As a follow up to my previous post. The languages are (in order): Java, Groovy, Scala, Ruby, and Python.

I find the Ruby example to be the most readable, however it does use a magic variable ‘ARGF’ to help its readability. What’s surprising to me is how I find Scala not that readable. At least, not that much more readable that Java. Its shorter, so that alone is helpful, However, its a little hard to see Scala as the replacement for Java if its not substantially more readable.


I found a simple example program done in five languages. I’m not saying which is which (but one is obvious). I find the “new” languages interesting, though none of them seem to be getting significant traction in my circles.

I’m posting this now looking for comments and I’ll have a follow up post soon.

Which example reads best to you?

example 1

    public class LongLines{
       static int MAXLENGTH=70;
       public static void main(String[]args)
             throws Exception{
          for(String arg : args){
             BufferedReader br = new BufferedReader ( new FileReader ( arg ) );
             String line;
             for(int lineNo=0; null != (line=br.readLine()); lineNo++){
                   System.out.println(arg+" line="+lineNo+" chars="+line.length());

example 2

 	def MAXLENGTH = 70
	args.each { filename ->
      new File(filename).eachWithIndex { line, lineNumber ->
        def length = line.size()
        if (length > MAXLENGTH) {
            println "$filename line=$lineNumber chars=$length"

example 3

    val MAXLENGTH = 70
    def process(filename: String) = 
      for {
        (line, lnum) <- Source.fromFile(filename).getLines.zipWithIndex
        if line.length - 1 > MAXLENGTH
      } println(filename+" line="+(lnum + 1)+"chars="+line.length)

example 4

    MAXLENGTH = 70
    ARGF.each{ |line|
       if line.chomp.length > MAXLENGTH
          puts "#{ARGF.filename} line=#{ARGF.lineno} chars=#{line.length}"

example 5

    MAXLENGTH = 70
    def process(filename):
        for i,line in enumerate(open(filename)):      
            if len(line) > MAXLENGTH:
                print filename, "line=", i+1, "chars=", len(line)
    for f in sys.argv:

None of these examples really show the robustness of their perspective languages I’m sure, but I think readability is very important and readable brevity is even more important (especially compared to Java).