Side effects are bad: a personal illustration

March 23, 2006

One of the things they tell you in programming school is 'side effects are bad; you should avoid them if possible'. Apparently every now and then I need a reminder of this, because side effects were at the bottom of my feed whoops from last night.

One of the most expensive bits of DWiki is converting pages from DWikiText into HTML. Originally the conversion was simple and straightforward; stick wikitext in, get HTML out. But as DWiki grew more features, the wikitext started having additional information: whether people could see it, whether people could comment on the page, and so on. These days converting a page to HTML generates all of:

  • the HTML
  • whether the page restricts access and/or allows comments
  • various fuzzily defined features of the page
  • the title of the page, if any

The conversion routine returned the HTML and recorded everything else by manipulating a per-request context structure; in other words, through side effects.

One of the problems with side effects is that you forget them, which is just what I did when I added caching of the conversion process. I remembered the other two side effects and cached them (partly because I'd been working in the area recently), but forgot about page titles. When the cached data was restored, page titles weren't set and things reverted to default titles in feed entries.

(One of the reasons I forgot about the page titles is the other problem with side effects, namely that they're usually scattered over the code instead of centralized in one spot. If the renderer had returned all of the results in one structure and broken them up later, I might not have wound up in the pickle I did.)

Update, April 1st: I just discovered another side effect I had forgotten; updating the timestamp for the request's Last-Modified: header. Oh the irony; apparently I like playing April Fools' jokes on myself.

Written on 23 March 2006.
« We apologize for the disruption in syndication feeds
Setting up to build RPMs »

Page tools: View Source, Add Comment.
Search:
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Thu Mar 23 23:20:28 2006
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.