Hacker News new | ask | show | jobs
by Borg3 440 days ago
Yeah.. and its annoying to read.. I always laught at people who try to make IDs with leading zeros. and then one day BOOM!! overflow.

Its especially common in namings like: THING-01 THING-02.. we will never have more than 100 of them.. and then BOOM.

I always say: leave it at fucking 1 and count up. Thats why we invented Natural Sorting to sort this out...

3 comments

The thing is... it's generally safe to truncate a leading zero [0], but it's not necessarily safe to truncate a trailing zero. For example, sometimes trailing zeros convey precision, and then you've got SEMVER [1] causing situations like Drupal 7.1 and 7.10 and 7.100 (spanning 100 minor releases).

[0] ZIP codes and phone numbers are important exceptions, but it's a non-issue if you always process these as strings, never as numbers, which is a reasonable constraint because we don't need to sort these numerically. Lexicographical sort is perfectly fine.

[1] The concept mentioned in footnote 0 does not really apply to SEMVER, because we do like to sort versions numerically. Lexicographical sort is wrong. But it's a group of dot-delimited integers, not to be conflated with floats, so while 7.100 comes before 7.2 when sorting floats, 7.100 comes after 7.2 when sorting SEMVER because the 2 and 100 are just integers.

Counterpoint: Natural sorting likely will be orders of magnitude slower sorting than ordinal sorting.

For most situations, the better solution is storing the index separately to the name in another column or in metadata.

But for some stores, there isn't an easy way to do to store or sort on metadata, and so prefixing leading zeroes helps keep things stored more naturally while using the more efficient sort.

Oh of course it is. But if you have serveral 100s up to several 1000s items, you do not care. Computers are here to do the heavy lifting. If we talk about millions of items, its completly different story. Probably simple numerical ID is out of option and you start to shard them somehow. Use right tool for right task!
ah.. so use THING-001. got it