4 min read

Species of code

(Half-hearted humor. The images for each section might be the best part.)

Code comes in a variety of species. Some species live less than a day, a week, or a month by nature.

Shell command

"Hidden Hermit Crab" by Jerry Kirkhart.

It's easy: Just type a command into Bash, cmd.exe, fish.

Lifecycle: Seconds, with a long tail.

Travels in bales, which sometimes evolve into...

Shell commands

"Green turtles in tidepools in Kona" by Brocken Inaglory.

Some shell commands prefer to travel together.

Lifecycle: A few hours, hopefully. If cared for, probably ends up in a batch program.

Pipe maze

"Maze of Pipes" by Flickr user olavXO from Norway.

(a.k.a. "wobbly pipe complex")

Sometimes you need to solve a very specific problem just once. Like: Rename five hundred text files spread across different parts of a deeply nested, unique directory structure. Or: I need to get these zip files out of that other zip file, then run a script from the zip file on the zip files I pulled out of the zip file, then I need to run my own separate script on the output of the first script. An easy way (easy, you'll think) is to chain together a bunch of shell commands with pipes. You're pretty sure you can do what you need with some combination of head, tail, cut, tr, sed, perl, sort, uniq, wc, jq and (oh no) find.

It's exactly that easy, and exactly that hard.

It's probably the best solution in many cases. Still.

This kind of code doesn't have to be general. It's nice if it's a little bit general, because you might have to copy it and change it to do something slightly different, but it's not worth the cost to make it completely general. I usually deal with this by assigning key paths or components to variables.

I try to write these down just in case. I mostly don't end up needing them again, but for the few I do, there is zero chance I would remember what I did a month after writing the thing.

Lifecycle: A few hours... I hope. If left uncared for, rusts and eventually falls apart. If cared for properly, becomes...

Batch program

Printing press at Goa State Museum, by Flickr user AaronC.

Sometimes this is a shell script. Maybe it is in Python or Ruby or Perl or even JavaScript. Maybe it is even a compiled language. You might even have code split over multiple files, or tests (fancy!).

In theory this species contains a whole range of things. For my purposes here I am mainly interested in the small scripts, but loads of programs operate this way.

Lifecycle: Months, years. I'm told these can last decades if properly cared for.

Subspecies: Lots. Download some data. Make calculations. Score that thing. Take that model and run it on that trashcan the data. Upload some data. Format some data. Reformat some data. Take that data and glue a bunch more data on. Preprocessing, postprocessing. interprocessing. Measure something to collect some data. Simulate something to collect some data. Take this data and make a pretty picture out of it. Take that text and make a document out of it.

This should have been a spreadsheet

Probably not this.

Sometimes you write code and in retrospect you think: This should have been a spreadsheet. It morphs several times an hour and grows new limbs in the dark. Its protuberances are found all over the filesystem: Data, metadata, cached intermediate steps, logs, more metadata, graphs. Shouldn't reproduce, but will.

It wasn't always like this. It started as one quick chart or data batch script, maybe with a few lines of debugging. CSV in and CSV out, that's good enough for me. One simple matplotlib chart is probably fine. It's a one-off, right, no need to think too hard. Maybe it could even have been called exploratory.

It has become so much more than that now. You needed more numbers, more charts, closer analysis. You needed to make that one chart stop clipping the most important (expletive) piece of text.

Lifecycle: Hours, days, or weeks after it was convenient. Could be more if you're unlucky.

Don't connect it to anything. Don't leave it in plain sight.

Web app

"Indirana semipalmata lower surface of foot," by "Boulenger, 1882."

Also known as a bunch of JavaScript and some HTML. If you're feeling fancy I suppose you could add a backend. A database, even.

Lifecycle: Months or years.