The best software shape
| path | blog / software shape |
|---|---|
| date | 2026-08-11 |
| tags | programming |
| desc | Describe the best software shape and its collision with reality |
Wouldn't it be nice if all software looked something like this:
js
function* input() {}
const handle = () => {};
const render = () => {};
let output = {};
try {
while (!input.done) {
output = handle(input.next().value);
render(output);
}
// normal exit
} catch (e) {
// abnormal exit
} finally {
// cleanup
}
Model input as a generator and our application differentiates itself with different handle and render functions. Handle is a pure function, and render is all side-effects. This describes the steady-state of a program. If handle really is pure, then we're guaranteed lots of nice properties.
But server.js doesn't look anything like this, and it's interesting to consider why.
Server.js
server.js is a relatively short 1000 LOC file that defines a very performant, feature-dense HTTP/S and WS/S server
and it looks more like this:
js
////
import http from "node:http";
class Server {
async initialize() {
bind(setup());
}
setup() {
return config;
}
bind(config) {
http.createServer(config, this.handle).listen(config.port, config.ip);
}
handle(request) {
return response;
}
}
new Server().initialize().catch((e) => process.exit(1));
Notice the lack of a loop. Notice the lack of stand-alone functions - these are all methods, and none of them
look particularly pure (although handle might be). setup() probably reads from the world, but does not write to it.
bind sets up the steady state, and at least only depends on its arguments (one hopes). Why is initialize async?
Why is this a class instead of a loose collection of functions?
The desire to be simple, to keep everything in one file, is in tension with the desire to define a library of pure functions and object literals and compose the application out of those components. The desire to support standard developer conventions, like not shipping a simple script, but instead defining a class (or function) and export it so you can use it in other contexts, is also in tension here.
Wrap events in a wrapper!
Some events, not all. Wrap them in a generator function, and then if you squint real hard reflector looks like the ideal shape.
The funny thing is that server.js was never actually the point of this project! It was supposed to be a throw-away
ultra simple server that would, eventually, just serve one resource: the Simpatico Shell. And yet it kept getting
features as it turned out to be a really handy, fast, useful server, especially it's support for Let's Encrypt TLS and
Literate Markdown. Eventually it even got multi-domain support, which I still think is overkill. I even
had the idea of using the essential primitive, the simpaticore, to build the server itself.
server.js's operation could never be a Simpaticore - at best, we could break down functionality into object pieces and layer them in as an
Stree. Maybe we could think outside the box and consider each page of the site a row in the stree, that slowly accumulates some sort of metrics. But the naive approach would be to accumulate every request as an object, every client as a row, and that would in general be too much data.
I'm tempted to sit back and imagine what my ideal code structure would look like for server.js. But after looking
at it, and all that it does, it's actually a pretty good compromise between lots of design forces. It's <1000LOC and
does a lot. That said, I would like to break it down into functions and write traditional unit tests for it.
Testing
An uncomfortable truth is that I wrote my own test harness (testable.js) that requires a browser to run. I did put some effort into making it work with headless chrome, but ultimately, this is more hacky than even I am willing to endure. In theory it's a good idea to support one runtime and support the hell out of it. And honestly it's a pleasant way to develop and test pure javascript code.
Literate Markdown
I really like this variant of markdown as a fast, self-hosted BTD loop with some clever developer affordances. But it still has rough edges like not supporting code maps. This means that exceptions etc in the dev console don't point you to the right place. This isn't really as huge of a problem as you'd expect, but it's a constant minor annoyance, that gets smaller the smaller the file you're working on. So it's a natural pressure to keep your litmd files short!
The biggest problem with it is that it doesn't compile. That is, the code you write in litmd is separate from
the eventual library js file. Moving code from one to the other is entirely manual, and this invites skew.
Another related problem is that the browser doesn't support exports from <script> tags, so they can't depend on
each other in the usual way. The work-around is to manually modify the window object and reference this
in subsequent code areas. But this is brittle and would make a hypothetical compiler even more brittle.
I've also noticed that I use litmd in at least 3 distinct ways: first, to inch my way toward a library or function. In this use case, independent code sections is usally a good thing, since I'm iterating on the design. The second way is to use litmd as a test harness, and the code-under-test is in another file and the litmd file demonstrates and tests its use. Sometimes these two mix a bit, which is fine since I just add test statements to the specific code block. The third (and which is most compilable) is to actually specify the library itself. Sometimes my design iterations yield the proper product. That is, in an ideal world I'd mark the last few code sections as "compile" and they'd be combined in some useful way into a js file.
For the most part I think it's wise to keep it simple, and introducing a bad compiler (one that operates at the string level) seems like more trouble than its worth. I might be wrong here though. Oh and the obvious place to add a compiler would be as part of the litmd renderer itself - instead of a 1-1 md:html in-memory render, we'd have a 1-2 md:html and md:js in memory and on disk renderer. Another reason to delay making something like this is that the simpaticore addresses some of these issues very naturally.
Devops
Another funny thing that happened accidentally is the introduction of minimalist "devops" support for systemd, idempotent updates, using cron to renew TLS certs, and so on. Everything on this front works well, with the key exception of TLS renewal: to this day, simpatico.io occasionally fails to renew its cert, and visitors will see an annoying message about visiting an untrusted site, etc. I can forgive myself, and even offer a little praise, for this addition since "leave it as an exercise for the reader" is not a very good way to get people to host their own code. It may seem cruel to offer a set of bash scripts and readmes for this, but I'd argue its honest - if you're hosting code, you should be comfortable with bash. This is true if you use a 3rd party package like Ansible, Chef, or K8s. Its the analog to writing Simpatico in modern JavaScript - it's something you really should know, regardless of your preferred application stack. I'll add that I'm rather pleased with the bash scripts - libraries of useful functions, composed in the scripts that use them.