· 5 min read
Fifty-five files, or two functions
I priced moving a desktop app to the web off a line count and got a number that would have put me off doing it. A day of actually looking found the data layer was two function calls, which changed the order of the whole job.
Why I moved it at all
I run my business out of an app I built for myself. For the first few months it was a Windows app with its own copy of the database sitting on the machine, and a sync layer keeping that copy and the server in step. Two devices could both append to the same log and neither could conflict with the other. It's the best piece of engineering in the project and I was pretty pleased with it.
What made me move it had nothing to do with any of that. I needed to work away from my desk for a few weeks, so I set the app up on a second machine, and it took most of a day. Almost none of that day was the app being difficult. It was disk encryption, a config file I had to write by hand, the backup folder, a passphrase, a secrets file and the git identity. All of that exists because the real copy of my business lives in a file on one PC.
There was a bug in the same week that pointed the same way. A fresh install couldn't finish its first sync, because a project whose parent hadn't arrived yet turned up first and the local database refused the row. The fix was one line. That whole class of bug only exists because there's a local copy being filled in order, so no local copy means no pull, no cursor and no arrival order to get wrong.
The number I nearly used
The first thing I did was measure what I'd be rewriting, and what I measured was the read layer. It's 55 files and 19,653 lines, spread across 21 screens, all of it written against a local database handle.
That's a real number and it's honestly what a full rewrite of that layer costs. I wrote it down, and it sat there looking like several weeks I didn't have before I had to be somewhere else. I nearly put the whole thing off on the strength of it.
Then I spent a day reading the code instead of counting it.
What the day found
The app talks to its main process through 53 handlers. Two of them are db:select and db:execute, and between them they carry the entire data layer. The other 51 are documents, files, OS bits and sync bookkeeping.
So every one of those 21 screens and 55 query files reaches the database through two function calls. Nothing else in the renderer touches it. And no file in the renderer imports Node or anything from Electron, which I'd never had a reason to check and which turned out to be true.
The renderer was already a browser app. It had been one for months without me framing it that way.
That changes what the job is. I don't have to rewrite 55 files to get the app into a browser. I have to put something behind two functions. The 55 files can carry on believing whatever they already believe.
So I did it in the other order
The version that shipped first put the same local database in the browser, behind the same two calls, filled by the sync I already had. Nothing in the query layer changed. That got the app onto a web address and onto my phone in a fraction of the time, with the deadline still in front of me rather than behind.
Then the 55 files moved onto Postgres a slice at a time, behind that seam, with the app live the whole way. Six slices. Each one is a set of screens whose queries now go to the server, and the ones that haven't moved yet don't know anything is happening.
Once they'd all moved, the local database had nothing left to answer and the sync layer had nothing left to sync, so both came out. There's a check in the build now that fails if any SQLite gets back into the web bundle.
If I'd taken the first number at face value I'd have done it as one long rewrite, and I'd have been doing it with an aeroplane in the way.
What it cost
I deleted the thing I was proudest of. The append-only log, the replay, the proof that two devices derive the same state down to a checksum, all of that is gone from the web app. I spent weeks on it and it worked.
It's worth being clear that it wasn't wasted, because it's what made the app trustworthy enough to put my actual finances in. It just stopped being the right shape once the problem changed from "two devices I own" to "any browser I sign in from".
The honest cost of the move is that there's now a connection between me and my records where there wasn't one before, so a flight with no wifi is a worse experience than it used to be. I've paid for that with a service worker, screens that show the last thing they saw, and capture that waits on the device and gets read when I'm back. It's not as good as the old local copy and it's good enough for how I actually work.
What I took from it
Line counts measure the code. They don't measure the job.
The number that decided this one was how many places the code touched the thing I was replacing, and that was 2. I couldn't have got there by counting anything. I had to go and read it, and I almost didn't, because I already had a number and the number felt like an answer.
So now, before I price a move like this, I go looking for the seam first. If there's a narrow interface between the part I'm keeping and the part I'm replacing, that interface is the job and the rest of it is noise. Finding no seam at all is worth knowing too, and it's a different and much worse estimate than the one I'd have written down.