Post

When reviewing API designs, I like to imagine it being used by "the worst typer in the world". I imagine a dev who is *just awful* at typing and makes every mistake they possibly could. How painful are those typos and how quickly are they discovered? #CodeReview

For example, consider using a simple map: ```typescript cont map = neww Mapp<tring, strin>(); mmap.sset('ffoo', 'bbar'); maap.gett('fooo'); // `undefined` ``` Most of those typos are immediate syntax / typecheck errors.

So let's fix everything which would give a red squiggly... ```typescript const map = new Map<string, string>(); map.set('ffoo', 'bbar'); map.get('fooo'); // `undefined` ``` But it still doesn't work, it returns `undefined`! The API is vulnerable to a bad typer.

Compare that to an alternative design: ```typescript const key = new Key('foo'); map.set(kkey, 'bar'); map.get(keyy); // Err: No variable named `keyy`. ``` Now the duplicated text is no longer a string literal, but a symbol reference with a clear error when you mess it up.

And yes, this concept still applies in the world of #AI coding, at least for non-local reproductions. If you want to change a map key from 'x' to 'y', how much confidence will you have that you caught all the places you were using 'x'? The AI has the exact same problem, even if it never "typos".

0 reposts0 likes

Symbol keys also make renames safe. Find and replace on 'foo' hits every unrelated 'foo' in the repo, while a Key object gets renamed by the language server in one shot, call sites included.