Skip to content

What Excel Taught Me About Data Structures

2 min read
excelwebdev

A couple of months into the JavaScript course at Geekwise, the instructor put an object literal on the screen. Curly braces, keys on the left, values on the right, a colon between them. I copied it down like everyone else, and it nagged at me the whole drive home, because I had seen it before. It's a two-column lookup table, and I have been building those in Excel for years.

The shape really is the same. A lookup table is a key column and a value column: code on the left, the thing the code means on the right. You hand VLOOKUP a key and it hands back the value. obj["ALWC"] and =VLOOKUP("ALWC", codes, 2, FALSE) read like the same sentence in two languages. Even the failures rhyme: ask for a key that isn't there and one gives you undefined, the other #N/A, and in both cases the real culprit is a trailing space you'll find an hour later.

The reading I've done since class says programmers call this kind of structure a dictionary, or a hash map. I want to be careful with those words, because I learned them about three weeks ago. And the more I read, the less sure I am that the resemblance goes below the surface.

Here is what VLOOKUP actually does, as far as I can tell. With FALSE at the end, exact match, it starts at the top of the column and checks every row until it finds your key. Ten thousand rows means up to ten thousand checks. With TRUE, the default, it expects the column sorted and does something smarter: check the middle row, throw away the half that can't contain the key, repeat. One write-up called this a binary search, and it explains two things I had only known as rules: why the default is fast on big tables, and why it returns confident wrong answers when the data isn't sorted. A hash map does neither, supposedly. It jumps straight to the value without checking the other keys at all, by doing something with the key itself that I don't understand yet.

So a lookup table behaves like a dictionary at the formula bar and is built like something else underneath. "Sort of" turns out to be the precise answer.

What I keep turning over is how much of my Excel knowledge is like this. Years of reports and inventory spreadsheets left me instincts that carried into JavaScript almost untouched: keys have to be unique or the lookup grabs the first match and never mentions the others; a blank is not the same as a zero; name things consistently or pay for it later. Those held up. But until this month I would have told you VLOOKUP "looks things up," full stop, and been wrong about what that meant in a way that never once showed in my work. Instincts like that hold right up until they don't, and from the inside I can't tell which of the others are load-bearing.

For now I keep a running list of things Excel taught me, with a note on whether each one survived contact with a second language. The list is a key column and a value column.

Lo