Hacker News new | past | comments | ask | show | jobs | submit login

> Nevertheless, in good hands, Java can be quite elegant and succinct.

Could you elaborate, perhaps with an example? Not trying to start a flamewar or anything - but I honestly can't imagine a case where Java is succinct relative to other languages. And as far as elegance goes, Java is so overly verbose and fixated on classes ('too many classes? there's a class for that!') that I have a hard time thinking of it as elegant.

But then again, maybe I'm just used to reading bad Java code everywhere, and you've been lucky enough to find the good stuff!




There's no way to reduce something like:

    runOnUiThread(new Runnable() {
        @Optional
        public void run() {
            // ...
        }
    });
It's just the nature of the language. I honestly don't mind recent versions of Java all that much but without an IDE I would lose my mind in about 3 seconds.

And once you start writing something significant or that needs to work cross platform say hello to design patterns to work around the straight jacket.

I'll take Lisp, JavaScript, Ruby, or Python any day of the week. All languages have warts but to me it seems that Java has warts by design.


One of my favorite things to do in java. Save anything to XML, it even handles all the objects your objects are pointing to. All you need to do is make your objects serializable. Then java handles everything else using reflection.

  public void saveAll(Object[] objects) {

	File file = new File(filename + ".xml");

	try {
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
		XMLEncoder xenc = new XMLEncoder(bos);

		for (Object o : objects) {
			xenc.writeObject(o);
		}

		xenc.close();

	} catch (FileNotFoundException e) {
		System.err.println("File not found");
	} catch (IOException e) {
		System.err.println("Some other error");
	}
  }


C# also gets knocked for verbosity (though it's a bit better than Java). But with manifest typing (which is optional since C# 3.0), judicious use implicit cast, and `dynamic` the language can be made concise and elegant.

An example from my library[1]:

    SqlCommandEx sql = "select * from person";

    foreach (dynamic p in sql)
    {
        var full_name = p.first_name + " " + p.last_name;
        var age = ((DateTime)p.dob).YearsAgo();

        //etc
    }
    //automatically disposes the connection

[1]: https://github.com/noblethrasher/Prelude


I don't know Java, but it's often worth trying this sort of exercise out. I'm open to the idea it might be perfectly possible.

I once took one of Peter Norvig's Python programs (a spelling corrector) and converted it into VB.NET, just to see whether VB was as succinct as Python. Turns out it is:

http://www.stevecooper.org/index.php/2010/11/10/vb-net-and-p...

Or finding out that C# anonymous functions are more concise than Haskell. Thing is, sometimes these things surprise you. Languages do get reputations that are hard to shake.


I would suggest reading Effective Java by Josh Bloch, it has some great tips on how to write code and design APIs as well as nicely structured sample code. I'm a much better developer (not just in Java) for it.

Also browse some of the core Java source code to see really elegant implementations by him.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

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

Search: