Neat Idea: Creating Alphanumeric IDs

UPDATE: For those of you looking for a great way to generate highly unique ID that is shorter than what you might get using a hex number, try this (it will generate a ~17 character ID):

list($hex, $dec) = explode(‘.’, uniqid(null, true));
$id = (base_convert($hex, 16, 36) . base_convert($dec, 10, 36));

Ever needed to create an ID has that looked something like f39a2xm91? You might not have, but some day you’ll want to. The easy way out is to use the native md5() function, but that creates a long 32 character hash which may be a total waste of (database) space. These types of IDs are often used to mask integer IDs so that your users can’t just type in user_id=10000, user_id=10002, user_id=10003, and so forth to look at your records. Some might even call it security through obscurity. Well, let’s be clear: this sort of activity does not add security, but it does make for making “browsing” behavior more difficult.

Either way, if you desire to move away from the classic integer format IDs, I have a different solution for you:

base_convert($someId, 10, 36);

This will convert the number 10,001 into 7pt, 10,002 into 7pu, and 100,000,000 into 1njchs. As you can see, you can store a heck of a lot of numeric information in a very tiny amount of (character) space. I am not saying this will save you database storage space, but it will make your URLs shorter.

One of the main benefits is this is that you can store more data in a smaller human-readable space, thereby allowing you to create smaller unique IDs. So for example, in our logging system at work, I use this method to generate issue IDs that end-users can send to us when they have a problem. This issue ID is based on the last eight digits of current time with microseconds concatenated with (using “.”) a random seven digit number in front. I then base-36 encode this resulting number (stripping out the decimal point).

Note: my solution is specific to the problem I was facing. It’s not necessarily a full proof way to generate unique values, but it’s what you would call “good enough”. Do not use the solution unless your solution does not hinge on absolute unique values.

A warning about base_convert is that large numbers breaks down in PHP, so be careful (we’re talking very large numbers). This means pasting together the current timestamp, the user ID, the session ID, and a fourteen digit number into one 50 character long number will probably result in some precision errors (not a huge deal for most implementations, but be warned). From the PHP manual:

This is related to the fact that it is impossible to exactly express some fractions in decimal notation with a finite number of digits. For instance, 1/3 in decimal form becomes 0.3333333. . ..

 

So never trust floating number results to the last digit and never compare floating point numbers for equality.

The “base” refers to the numbering system used to convert the number. In a base 11 system, the counting goes to 10, then the letter A, and then loops around back to 1. So in other words, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, A, 1, 2… In base a base 16 system, you would go all the way to F before going back. So the larger the base, the more “compressed” a number can become.

I used base a 36 scheme (0 – 9, A – Z), but you can use smaller bases to come up with longer conversions. For example, a base 21 conversion (0 – 9, A – K) will convert 10,001 into 11e5, 10,002 into 11e6 and 100,000,000 into 13a3k7g. So in short, if you have a database where your record IDs start at above 7 or 8 digits, maybe you can think about base encoding them into shorter IDs.

Just a neat idea I wanted to share.

My Thoughts on Microsoft Buying Yahoo for $44.6B

The big news of Friday morning was that Microsoft offered Yahoo $44.6 billion for the company. On a financial level, this is a sweet deal for Yahoo. It’s not the most financially sound investment Microsoft has offered, which is why their stocks dipped 6% on Friday. No reply has been made from Yahoo, but I can definitely see them taking this offer seriously. My thoughts are summed up in three bullet points:

  • Yahoo’s management will possibly accept the offer since it is so lucrative.
  • The purchase will piss off some of Yahoo’s top talent and cause them to defect, possibly probably to Google.
  • The purchase will help Google gain a greater lead during one of the most crucial eras since the Internet began: the rise of mobile computing.

The internal culture of Yahoo is not exactly friendly to Microsoft. Yahoo is seen as an ally to the open source community while Microsoft is exactly the opposite. Yahoo is a major contributor to open source (ex. PHP’s lead developer is on Yahoo’s payroll), has an open philosophy which has shown itself in their JS frameworks, Flickr, Pipes, and various other projects, and is a major user/contributor to the open source stack in general. Microsoft is clearly not on the same page.

I’ve read speculation that the looming recession will cause developers to stick around despite a take over from a boss they don’t like. However, my belief is that great developers aren’t scared to leave since they are in high demand no matter what is going on in the economy. Some of the very best and brightest at Yahoo will leave. Any sort of exodus of major talent would destroy the current internal direction. Worse, some of these great minds would likely go knocking on Google’s doors, which is straight up ironic considering Microsoft’s intentions. This leaves gutted, possibly begrudging or de-motivated teams, recipes for not producing innovation.

Which leads to my final point: Microsoft’s goal is to beat Google by merging with Yahoo’s resources. It is my belief that this move could ultimately prove counterproductive. The integration process of merging departments, axing un-needed employees, changing internal processes, shifting internal priorities, introducing new management, and replacing fleeing key talent will cause major stalls over in Yahoo… At Google’s benefit. Microsoft is no stranger to mergers and acquisitions, but Yahoo would be a major, major purchase with a sizeable employee count. Microsoft will have its hands full for months.

All this is going to happen during a period I consider to be a key moment in the rise of mobile computing. A large chunk of search traffic will begin to come from mobile browsers, and the web will shift to the mobile platform. During such a crucial stage of computing, this sort of disruptive purchase may help Microsoft and Yahoo miss the bus.

So while I wouldn’t be surprised if the floundering leadership at Yahoo took the offer, I also expect this to work out as the most counterproductive and costly purchase in Microsoft’s history.