K Code Parser (story 5)

Where’s story 4? Well, it’s coming. Probably in an episode named ‘the missing story’. Who knows? 🙂 Half the story is written in mercurial commits, the other half will be written later.

So today, about a new cool thing. Not that new, in fact, but pretty new to me.

I was always baffled by the need to iterate through collections. I always loved the idea of foreach statements, and this morning I found the ‘foreach’ of Java. It’s ‘for’, and it looks like this:

compare this with:

While the difference might not be too big, it definitely feels big to me. Because, for example, every time you do a get(i) you might get something that’s checked for bounds, maybe not O(1) complexity (if you change the data structure for myArrayList without notice) etc. And the point is that all you want to do is do something for every element of your collection. No need to extract data from an iterator (as you get data directly from the source, from a hidden iterator that probably is more optimal than anything externalized) – and it feels a lot better than the indexed code.

So I like this feature very much, thank you; I wonder, however, how readable I will find this at 3AM in the morning. I’ll have to set my clock and wake myself at that hour, just to have a look at that piece of code.

Comments are closed.