Syntactic sugar: why Iterators have no place in code. (i.e. Java needs to learn from Ruby)
Posted 7 August 2006 @ 6pm | Tagged software, thoughts
So here we are in Java 5, some ten years later, we finally have a sugar coated syntax for for loops.
for (var : Collection) ...
Now the embarassing thing is, even Visual Basic had a For Each construct like 8 years ago. Java seems like A Tale of Two Cities, a book for which Dickens was paid by the word. Are Java designers being paid by the byte to write monstrosities like:
for(Iterator iter; iter = collection.iterator(); iter.hasNext()){ ... }
There is a primary reason that syntactic sugar matters: we write code not for the self gratifying purpose of putting words on a page, but for solving real world problems. The closer you get to the language of the customer domain, the faster and more accurately you can write code. If a customer tells me that for each order we need to email the manager, I want to write code that says
orders.each {|o| email o.manager}. I don’t want to write something that starts with “Iterator…”.
Syntactic sugar matters. Beautiful code matters. Domain specific languages matter. The Java community needs to start realizing this sooner rather than later or face obsolescence. Iterators have no place in the real world. Abstract them out.



No Comments Yet