Tuesday, October 24, 2006 #

FireFox 2.0 clumsy iterators

Has everyone already nows, FireFox 2.0 is shipping this afternoon. It seems to be a nice release, and I'll surely use it often as soon as it's accessible.

I was looking at the new javascript 1.7 features. Especially the iterator thing, that I use a lot in C# since .Net 2.0 and that will be used even further with C# 3.0.

I was really sad to see how poorly it has been designed in javascript 1.7.
It's ok to move to next item in the next() method. But why did they not provide a way to test if there is more items in the iterator ???

When enumerating an iterator you have write something like this :

        var it = Iterator(obj);

 

        try {

          while (true) {

            document.write(it.next() + "\n");

          }

        } catch (err if err instanceof StopIteration) {

          document.write("End of record.\n");

        } catch (err) {

          document.write("Unknown error: " + err.description + "\n");

        }

There are two ugly things about that :

  • having to write while(true) which should be something rather rare.
  • having to catch an exception for a case that is really not exceptionnal ! You should never use exceptions in your main control flow !
Why did they not add a moreItems or any other name property to know if you can call next(). You would write :

        var it = Iterator(obj);

 

          while (it.moreItems) {

            document.write(it.next() + "\n");

          }

And it would be sooooooo clean !

But they'll release it, and any poor web developper will have to deal with this stupidity till FireFox 2.0 becomes obsolete in something like 6-7 years... F...k !

Skup

posted @ 4:57 PM | Feedback (27)