Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm looking to create a database of goods and I don't think an incrementing id would be adequate:


So with 4 digits I can address 10000 items IIUC. I don't I want to show how many items I have in total or make it too guessable was the series is.

Of the examples of similar design I'm after I found:


I think I like bit.ly the most, though the uppercase letters might be error prone. IIUC with bitl.y's design you can address Upper and lowercase plus numbers to the tune of 5 digits. 62*62*62*62*62=916 132 832 which is great size to work with.

I am not sure what the theory of this study is called. If you could point me to some good links on the topic I would be grateful. I want the codes to be short and usable as permalinks.

I'm wondering why Google plus settled for a very long 21 digit user id. Are there 3rd party tools or database features from MariaDB or MongoDB that sort of automatically generate sane short codes for you?

share|improve this question
Why are you using leading zeroes? What's the point of that? Why are fixed-length numbers important here? –  S.Lott Jan 22 '12 at 12:58
Emphasize 4 digits type structure. –  hendry Jan 23 '12 at 2:59
If you can't explain why they must have this limitation, then I would suggest that the limitation is is Very Bad Idea. And down vote the question because you're causing your own difficulties for no sensible reason. –  S.Lott Jan 23 '12 at 10:42
Crikey you're pedantic. Down vote my own question... sheesh. –  hendry Jan 24 '12 at 4:37
Leading zeroes, admittedly they're unusual, but arguably they have a place in URLs as much as they have a place anywhere else. It subcommunicates the size of the total space, it means they look nice next to each other. A separate argument for fixed four digits is it reserves the address space of 1-3 digit paths for something else. All this assumes you can really be sure it will stay at 4 digits long-term as you don't want to go changing permalinks later on (to 5 digits). –  mahemoff Jan 24 '12 at 11:02
show 4 more comments

closed as primarily opinion-based by MichaelT, gnat, Dynamic, GlenH7, Bart van Ingen Schenau Mar 23 at 17:21

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers

You want a base N encoding library/function, e.g. a base62 encoder will give you the 26 lower-cases, 26 upper-cases, and 10 digits. Then you just apply it to a counter variable, the number of existing elements, or the new item's ID.

In pseudocode:

shortID = base(number_of_existing_monkeys + 1)
save(new Monkey(shortID, url, description)

(This is using number of existing monkeys, you could alternatively use an auto-incrementing counter variable.)

Here are PHP base62 encode/decode functions for example: http://snipplr.com/view/22246/

As you can see, libraries like this usually have a character array somewhere, so you can just edit the array with your own character space.

As far as Google using 21-char IDs, that's really something different. It's a GUID (http://en.wikipedia.org/wiki/Globally_unique_identifier). The idea is to generate something guaranteed to be unique even without looking at existing records. It means you can generate IDs offline and without having to worry about transactional integrity. It's convenient for when you want to work with the new ID, maybe even save it somewhere else, before the object has been persisted.

share|improve this answer
I can see GUIDs being useful for general data applications. But using them instead of a Google username in the G+ example feels very wrong to me. Like using IPv6 addresses instead of DNS. That would be bonkers. :) –  hendry Jan 25 '12 at 9:57
add comment

If you are concerned about upper/lower case causing confusion then 6 lower case letters and/or digits (base 36) gives you over 2 billion entries to work with. This nicely covers the address space of positive 32 bit integers.

You can use a numeric ID (positive 32 bit integer) which can be generated randomly if you are concerned about letting people know how many products you have. Then just convert the integer to a six character base 36 number.

Note that randomly generating your integer key may have repercussions if you actually create new entries frequently enough. You can poke around on SO or dba.SE to learn about the pros and cons of monotonically increasing vs. random clustered indexes.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.