Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

There are so many things wrong with your example! You basically wrote the ugliest Java code you could possibly think of for this simple program.

First, in order to get an objective comparison of code length you should at least use the same (length) variable names. You used "lineTokenizer" in your Java example and "l" in your Ruby example!

Second, you could have just written System.out.println(sum) in Java. The fact that you had to write System.out.println(Integer.toString(sum)) shows your lack of basic Java knowledge.

Lastly, the brackets around your one-line while loop in Java are unnecessary, unlike the brackets around your closure in Ruby.

Also, the Java main method declaration is irrelevant to your point, since it's written only once per program, and is automatically created by any IDE.

How about this equivalent Java code to match your Ruby example:

  int count = 0;
  Scanner s = new Scanner(System.in);
  while (s.hasNextInt())
    count += s.nextInt();
  System.out.println(count);
Not so different now, is it? You can write concise code in any language if you know how to use it.


Um... I didn't write the example. I took it from a site that encouraged developers familiar with a language to submit solutions.

The developer of the Java solution posted something I'd consider reasonably representative of the Java I would likely encounter from most Java programmers and I while I have almost no personal experience with Ruby, I have the feeling the Ruby solution is reasonably close to what Ruby programmers write.

While you can write concise code in any language, it seems few Java programmers bother to.

Perhaps you should submit your solution to the original site: http://shootout.alioth.debian.org/


Using an example from a benchmarking suite to illustrate language conciseness doesn't make any sense. We all know that conciseness is inversely proportional to speed. Whoever wrote that Java code was trying to write the most performant, not the most concise code. He did a great job, by the way, since for that particular benchmark, Java outperforms Ruby by a factor of 20! (http://shootout.alioth.debian.org/gp4/benchmark.php?test=sum...)

Sorry for initially assuming that you wrote the code. I wasn't familiar with that site, so I didn't register your original allusion to it.


Au contraire, the very fact that the programming benchmark site neither rewards or punishes conciseness makes it a good place to see how important it is to the average programmer when also dealing with an explicit performance criteria.

Also, I think your statement that conciseness is inversely proportional to speed is rarely true. It certainly doesn't hold for the programming examples at the shootout site.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: