5 ms·
The rule of thumb I teach my students is that it's a number if you plan to do math on it—whether that be incrementing it or multiplying it or making less-than c
by blahedo 8y ago
The rule of thumb I teach my students is that it's a number if you plan to do math on it—whether that be incrementing it or multiplying it or making less-than comparisons (with number semantics). If the only thing you ever do with it is use it as a lookup key (or other direct equality comparisons), string might be a better way to think about it. Aside from the input interface issue surfaced in this article, values that are semantically numeric also have the property that they generally can omit leading zeroes (hope you didn't want those New England ZIP codes) or back off to inexact representations when they get large enough (sorry phone numbers whose area code starts with 4 or greater).
Yes, you can work around all of these issues by overriding default number behaviour. But some things that look like numbers are really just strings of digits.
- baroffoos 8y agoThats a good rule for dealing with messy real world systems like post codes and phone numbers but its still fine to use numbers in strictly defined non math systems. My database id column uses numbers and I have never had to do math on them other than ordering by id. The only potential issues I would look out for is things where leading zeros could be important and a database would probably drop.
- rickycook 8y agoyou increment your database ID for each row... 9-10 is natural for numbers but doesn’t flow for strings. making the number into a string is pointless because it always starts it’s life as a number
- yen223 8y agoWhat I find most of the time is that you only need IDs to be unique. The fact that databases use auto-incrementing integers to ensure uniqueness is an implementation detail, and not a strictly necessary one (think UUIDs).
- ryanbrunner 8y agoRight, but I think the main point is if you're going to use auto-incrementation to provide values for your IDs, you might as well use numbers, since at that point you are doing math on them (one super simple operation of "+1", but math nonetheless.)
- dalore 8y agoIf you use UUIDs you can have clients distributed around generate IDs for the db before they even save, no need for a +1 centralized id mechanism. That becomes a bottleneck.
- KineticLensman 8y agoI seem to recall a recent discussion pointing out an issue with leading zeros in IP addresses being interpreted as Octal, leading to possible invalid addresses like 09.09.09.09 (9 isn't valid Octal). So just because something looks like a number doesn't mean that you can do 'safe' things like add leading zeros.
- josteink 8y ago> I seem to recall a recent discussion pointing out an issue with leading zeros in IP addresses being interpreted as Octal, leading to possible invalid addresses like 09.09.09.09 (9 isn't valid Octal). That looks like a framework-specific issue and if that’s how you process incoming numbers from the Internet, you are clearly doing something wrong. Anyone on the Internet will reasonably expect that a decimal number provided to a Internet input-field will be interpreted as a decimal number. Anything else, unless explicitly stated, is by definition an error on the part of the application developer.
- KineticLensman 8y agoThe person concerned was inserting leading zeros so that the numbers lined up nicely, IIRC
- thomasthomas 8y agoSimilar to the rule of thumb I use: if adding 1 to the number gives you anything meaningful its a number.