Jonathan Strange and Mr. Norell: Magic and Programming

I just finished reading “Jonathan Strange and Mr. Norrell” (for the second time). It’s a really great book and it’s often described as Jane Austen meet’s Tolkien. If you like just one side of that equation you should still give it a chance as I found that it’s a strong enough book to overcome any distaste you have for fantasy or Victorian drama, so long as you like the other.

There are countless (better) reviews than I could give though, rather I wanted to write about one particular aspect of the book that was very interesting to me personally. I was struck with the similarities between the way that spell casting is described in the book and certain aspects of programming. I’ll do my best to avoid spoilers.

Particularly difficult or complicated spells are sometimes scripted, which is to say that they are literally written down on scraps of paper before being used. There are several scripting languages in popular use today. The most widespread and familiar to the reader probably being Javascript which is primarily used by web-browsers to enact all sorts of dynamic behavior on a web page.

Sometimes they are read from a book in the library of one of the magicians. In programming we often use code from libraries for the exact same reason as magicians in the book. Chiefly, we don’t need to take the time and to discover how to do something and secondly, we can avoid making mistakes by using code that has already proven to work.

When spells are not scripted beforehand it’s usually appears to be because the magic itself is either very simple or else there is simply no time to script a spell. This sort of seems like entering commands directly into a console. Maybe I’m stretching a bit on this analogy, but I didn’t want to leave you with the impression that all spells are scripted.

Spells have variables, which is another way of saying that spells are reusable, not single use. They don’t seem to accept arguments in the way that functions do. Rather I think they act more like closures (again commonly found in javascript) where the function is declared with a certain set of variables which it holds onto and use when it’s executed. Additionally spells, may also be embedded within other spells.

Spells can also be used as more than a simple script that is executed immediately and completed and then ends. Sometimes a spell brings into being a magical state in an object that can be interacted with. The best example of this is a spell that occurs multiple times in the book for finding a person. A silver bowl is filled with water and the magician utters a spell and then draws four lines on the surface of the water, dividing it into quadrants. He names each quadrant an area of the world to search in for the person. For example, England, Scotland, Wales, and Other. A blue dot appears in whatever named quadrant where the person is to be found. The magician then taps that quadrant which then zooms to fill the entire surface area and begins again to divide and name regions within the surface of the bowl. The magician does not need to restate the spell each time, rather he’s interacting in an iterative way with an object that has a magical state. It remembers the name of the person he’s searching for. I suppose there are a number of ways this could be implemented by a programmer, but I thought immediately of objects.

Lastly, spells have bugs and unintended consequences. Sometimes they last longer than they should, sometimes the effect of the spell effects unintended people or areas. Sometimes it doesn’t work at all and the magician doesn’t know why.

I’m sure there are more similarities, but these are the most obvious ones to me. I highly recommend the book for programmers (who tend to like fantasy and magic). Until it wears off my job will feel a bit more magical.

Written on August 1, 2014