Hacker News new | ask | show | jobs
by vitaliyf 2310 days ago
Similarly, here is your reminder than US Zip Codes aren't numbers, because many of us here in the Northeast have them start with a zero (or even two). So don't store them as integers, because Boston is 02108, not 2108.

See https://en.wikipedia.org/wiki/List_of_ZIP_Code_prefixes

2 comments

My general rule is if you can't add two values together then they shouldn't be represented as number types.
I once saw an integer overflow impact production because someone was concatenating primary keys together as strings to form new primary keys (and storing them as an integer type).
So a temperature is a number only as long as it's expressed in kelvins?
Perhaps you should take differences instead? Same with pointers. Their addition isn't meaningful, but their differences are.

In general, addition is only meaningful in linear systems, but differences can be meaningful in affine systems.

you can add farenheihts or celcius.
You can meaningfully add or subtract degrees to a temperature reading, but you can't meaningfully add two readings together (unless you're averaging them!). There's a subtlety here: A temperature difference is actually a different data type than a temperature. Dates and times have similar issues: You can add 2 hours to 3 hours, and you can add 2 hours to 3 o'clock, but you can't add 2 o'clock to 3 o'clock.

A better litmus test is whether you can subtract values. The difference between two times, or two dates, or two temperature readings is something that is meaningful and commonly used in everyday life. The difference between two ZIP codes is nonsensical.

> unless you're averaging them!

Nitpicking, but that seems like a great case for them being numbers. You will never average credit cards or zip codes. Averaging times is meaningful too (e.g. the mean arrival time).

I think op meant "adding" as generally doing math on them. Each quantity will have its own natural operations.

I see what you mean though, subtraction seems like a good heuristic by virtue of difference being mostly (always?) meaningful.

> The difference between two ZIP codes is nonsensical.

Not completely, there's a rough east-to-west pattern: https://d33wubrfki0l68.cloudfront.net/979d9b9012d8bbb1e4f14b...

So the difference is extremely crude geographic distance.

/evil

Too bad the post office wasn't aware of space-filling curves when they started assigning regions :)
Yeah, it's 5°C warmer than it was yesterday.
That’s the difference, not the sum.
Some are yet to get the memo. Not so long ago, I've seen a TypeScript course that demonstrated union types with an example of representing zip codes as either strings or numbers…