The jQuery PeriodicalUpdater, which has been getting lots of love because of feedback and support over here, has a new feature. The “data” configuration argument now will work properly with numbers and anonymous functions. This means that you can have your periodical updater send different information across in different runs.
I’ve said before that null sucks, but now I’ve got a case in point for why mutable data sucks, too.
One of the things that made this somewhat tricky was the fact that jQuery modified the configuration arguments passed in. The configuration arguments are just a map, and jQuery uses that map’s entries as scratch space for its calculations. This is really a problem if you want to use the map over and over again (as the PeriodicalUpdater wants to), because it means that the previous version of the data is ruined. (More description of the probem in this comment). The result was that I had to clone the arguments that I passed in so that I could reuse the arguments next time!
Note that there’s no documentation about this happening—in fact, it’s purely an implementation detail. But it’s an implementation detail which leaked out and caused bugs. I think there’s a real point to Clojure’s seemingly neurotic tie to immutability. It prevents a lot of problems like this.
3 Comments
You probably already know this, but it’s actually an entire paradigm:
http://en.wikipedia.org/wiki/Functional_programming
http://en.wikipedia.org/wiki/Referential_transparency_(computer_science)
Yes, I’m very aware. You may want to check out the blog posts under “Most Popular Posts” in the left sidebar of this blog, because functional programming evangelism and discussion is one of the main themes around here. Part of the reason I’m bringing these cases to the attention of the readers is to help people get why I am so hung up on functional programming.
One of the things that surprised me back at the last JavaOne is how far Clojure gets with immutability and dynamic typing. I’ve advocated before that static typing and functional programming go hand-in-hand (see the Functional Programming, Typing, and Closures post for more on that), but Clojure’s making me re-think that somewhat. It’s amazing how far they get with just immutability.
Ah, apologies, I’m a new reader :)