Writing Code vs Writing Fiction
Today's assignment, compare and contrast writing a story with writing a software.
Late last Monday night — about as late as possible while still being Monday — I completed a two-week binge of devoting nearly every waking moment to working on a software project. I was under no geas but mine own, this was a hobby project not unlike building a ship in a bottle — a painstaking detailed project done for the sheer joy of the exercise of skill and the perceived beauty of the final product.
That final product isn’t the point of this post.1 What I want to discuss here on Substack are the similarities and differences between the focused and intense coding I did last week with (my perhaps defective understanding of) what authors writing fiction do. One commonality I assume is the ability, even requirement, to sink deeply into an almost meditative creative state that erases time. I frequently lose track of time when deep into coding.
My thought going into this is that programmers and authors do the same basic thing in terms of the act of writing. The content is quite different, obviously, but I suspect it may turn out to be the notable distinction.
In what follows, I’ll use “programmer” to mean writer of software, “author” to mean writer of fiction, and “writer” if conflating them.
Lines of Code versus Words or Sentences
I’ve long compared writing a computer program with writing a novel. Depending on how big the computer program is, the comparison can be anything from a short story to a multi-volume series. Large programs like Windows™ have tens of millions of lines of code. In comparison, the Lord of the Rings trilogy, plus The Hobbit, weighs in at slightly over half-a-million words.2
On the smaller side of things, I’ve written simple but useful apps with fewer than 100 lines. The average (based on a quick spot check) is around 200 to 500 with some in the low thousands. The software project I finished has just under 4000 lines.3
But lines of code don’t seem equal to words of fiction.4 I think they compare more to sentences, though it depends a bit on the sentence and the line of code. Here’s an example of a powerful single line of code:
records = [rcd for rcd in sorted(database) if rcd.date < date(2023,1,1)]
It won’t mean anything to you unless you speak Python, but that line of code extracts records from a putative database, keeps only those older than January 1, 2023, and saves them into a sorted list. The previous sentence obviously described the same thing, so there seems some equivalence. Another example:
x = 0
This just sets “x” to zero. Which is also described in a sentence. No doubt there are some mismatches, but my guess is that, on average, the equivalence is fiction sentence to line of code. But according to Substack, up to this point I’ve written 27 sentences (28 now). A 28-line program (or 29 now) doesn’t seem like much, but how much have my 29 sentences accomplished so far?
Another aspect of programming makes the comparison challenging. In my files, I found a 48-line program that creates this chart:
Which may sound impressive, but the code leverages a large code library (matplotlib) to actually make the chart. The 48 lines just set up the chart data and appearance. It might take about the same number of sentences to describe the chart.5 Leveraging libraries aside, there still seems some equivalence between sentences and lines of code.
This may point out a difference between authoring and programming. Code often — perhaps nearly always — leverages other pre-written code. Even the 50 million lines of Windows code depend on BIOS of the hardware. Authors get into trouble if they casually “import” the sentences of others. At least without attributing it. (I suppose the programmer equivalent of that would be paying license fees to use a commercial code library.)
Attributed quoting (or implied attribution) isn’t unusual, but there’s a huge ratio difference. Fiction authors (as far as I know) generally don’t import large amounts of other sentences as quotes. A character may quote a line or verse, but even the longest quotes I’ve seen comprise only a tiny fraction of the text. In contrast, as the chart above demonstrates, a programmer’s code can be the tiny fraction of the whole.
So, the spectrum from smallest to largest program seems to roughly match the spectrum from, say, poems and jokes, through short stories and novellas, to novels and epic multi volume series. And I wonder if the distribution of writers in both crafts isn’t somewhat similar, too. Not many create enterprise applications or epic series, but many write shorter stuff. Almost everyone dabbles in things like Haiku and Limericks.
A difference here is that large software projects almost necessarily involve teams of people. Windows 10™, with its 50 million lines of code, required about 4000 people (and about 30 years). George R. R. Martin, as far as I know, works mostly alone (and, as I understand it, still hasn’t finished). The largest single software packages I’ve written on my own probably don’t break the 10,000 lines mark (or not by much, if they do).
Returning to Lord of the Rings, if I assume an average sentence length of 15 words, the trilogy plus prequel amounts to just over 38,0006 sentences. My bigger apps, then, are approximately equal to one volume of the trilogy. Which doesn’t sound too off the mark considering the sweat and time and what they do.
Much of my earliest work is on paper, and I’m not sure I have a copy of everything I ever “published”, but I’m sure over my career it amounts to somewhere near a million lines.7
Code Bugs and Typos and Errors (oh, my)
Authors are (from what I’ve read) embarrassed by errors in what they write. No one likes making mistakes.8 In contrast, some cultures believe perfection isn’t in the human purview, and therefore deliberately leave mistakes in their art. In honor of that concept, I’ve sometimes let a typo in a post stand (especially if it’s an amusing one).
But that’s not acceptable in code, which depends on being perfect. It rarely is, though. The difficulty of spotting all typos is similar to the difficulty of catching all bugs. Spell and grammar checks for authors are matched by code-checking and testing for programmers. These tools go a long way in reducing bugs and typos, but errors are ever with us (hence the cultural perception that perfection isn’t in out purview).
Logic errors are a more insidious form of mistake. Tony Hillerman has written about discovering logic errors in his books (often because readers point them out). In one case I recall him mentioning, his character was in a hotel room and had removed his shoes. But needed to quickly escape out the window. With no mention of grabbing his shoes.
Logic errors in code can be equally hard to spot. Unlike typos or obvious bugs, both of which often announce their presence, logic errors can lurk, only to be revealed when the program does a certain thing. Even more insidious, they may never seem to break anything, but quietly corrupt data or do something wrong but hard to notice.9
Utility versus Art
I said above that content seems the key difference between fiction and code. I think it boils down to the difference between utility and art. Some things have both in strong measure. Beautifully architected buildings and bridges are one example. Their primary purpose, in most cases, is utilitarian, but they were designed to also be beautiful.
From the other side of the coin, an artist might create something intended to be appreciated as art, but which also has utilitarian value. An example might be a musical instrument that deliberately stuns the perceptions, but which is unwieldy or impractical to play seriously.10 11
Paintings, sculptures, musical pieces, and texts (from poems to epics) rarely have a utilitarian value, certainly not in their primary intention. Sculptures and large books can make interesting doorstops (or burglar whackers), but that’s hardly their intended purpose.
Code almost always is utilitarian. I have written code fragments intended for the background of some graphic I was making, so code can be just art, but that’s a special case situation. On the other hand, a programmer can bring art — usually in the form of elegance and originality — to the design of the software and the data structures, as well as the way the code is written.
I’ve compared programming to architecture and building because both move from idea to design to construction. I imagine many fiction authors likewise move from idea to design (of characters and plot) and then to construction (writing). And there are as many ways to do that with coding as I imagine there must be with writing.
The above section about errors factors into this. Errors in art are, at worst, embarrassing but also possibly endearing, charming, or quaint. They can be accepted or shrugged off. But errors in code break the code — they can’t be shrugged off with a blushing “Oops, my bad!” Code is mathematical. Any bug eventually causes something undesirable to happen. A math error big or small. A benign bug may just render something on the screen wrong, but nastier ones can corrupt or lose your data.
Data and Code versus Characters and Plot
One of my favorite aspects of coding is designing the data structures, and I imagine this is something like designing fictional characters. There is, for instance, a sense that different ones need to be actually different (or they’d be confusing dual instances of the same thing — when confronted with such, writers often combine them into one).
I’ve taught data structure design, so I know it’s one topic that picks out those who understand abstraction that expert programming demands. The authorial equivalent might be having deep insight into the psychology of one’s characters.
In computer science there are important distinctions between code and data as well as the observation that which is which can be context dependent. To a compiler, code is just input data, and its output is the same data (same code) in a different form. In a story, characters and plot seem trivially distinct. And context-free. Who is a character and what the plot is don’t seem dependent on how I look at them.
I suspect the lack of comparison on this point comes from programming being abstract where fiction is concrete, more part of the physical world. Software can simulate the physical world, but with some major caveats.12
Conclusion?
I know coding, some 47 years of it. But I don’t know authoring. I’ve never thought I had it in me to write fiction. It seems much harder than writing code or blog posts. Fiction is an arcane art to me. As perhaps programming is to others.
What do you authors (and other programmers) think? Does a line of code seem to equate to a sentence? What contrasts or comparisons did I miss? Am I on the money or off the mark? Batting 1.000 or not in the park?
Until next time…
If you’re curious, you can read this blog post from my other blog.
576,459 words, according to Google.
3,916 lines, according to my own tools.
There is debate on how useful they are in measuring code. They provide some metric of size within a language but can be meaningless between languages. And project managers with lines of code quotas quickly discover LOC can be gamed.
But I’ll take the lazy way out and leave it as an exercise for the reader.
38,430.6
But I could be off. My post-retirement collection of Python code has 144,391 lines, and I’ve been retired 11 years. I don’t code nearly as much as I did at work. If I allow ~5 times as many work lines per decade, it does rough estimate to about 1.5 million.
Though they’re important to learn from.
One such insidious bug killed 4 and seriously wounded 2.
A great example is Steve Vai’s Hydra guitar. I’m not knocking it; I think it’s awesome! But it’s more a work of amazing techno-musical art than a practical instrument.
As an aside, Halloween costumes are often like that. Stunning but impractical. Three rules to remember about Halloween costumes. One must be able to conveniently: eat/drink, use the bathroom, and sit down.
Which I’ll be discussing when I write more on computationalism.
“his character was in a hotel room and had removed his shoes. But needed to quickly escape out the window. With no mention of grabbing his shoes.”
That’s a big one for me too. This is why I rely on my writing group. And when I think of these types of errors, I do think of them as a sort of video game that needs to be beta tested to make sure the characters don’t start running into walls and glitching.
Wyrd, you did some fantastic writing there. Well done. Being a total blockhead in programming, I haven't a clue what you said, but it was well written.
Back in the days when I was studying Accounting, I had to take computer programming. Fortran and Cobal. I'm dating myself; I know. PC's weren't on the scene in those days and computers were about the size of a small house. Why I needed to learn how to program one of those monstrosities I will never know! That's what computer programmers did, NOT accountants. Regardless, I studied Fortran until the cows came home! Passed the necessary exams and have never looked at computer programing since then.
Writing I understand. Programing I don't. You, my friend, are talented in both areas.
Well done!