135: Data Types: GUIDs Globally Unique Identifiers.

Take Up Code - Un pódcast de Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes

Categorías:

When you want to identify class instances or data records, you can’t use things like passports. Yet it’s just as important to keep track of object identities as it is for people. One simple way to keep track of things is to just use a number. Even a simple unsigned integer should give you over 4 billion numbers. Just start at 0 and increment each time you need a new one. You can even start at one just in case you want to reserve zero to mean something special. There’s lots of problems with this approach especially when you need to coordinate unique identifiers between multiple threads or even multiple computers. A GUID solves all these issues in a simple way that not only works now but can scale later when needed. GUID stands for globally unique identifier. and you might sometimes also see them referred to as a UUID which stands for universally unique identifier. GUIDs are huge. They use 128 bits and an article on Wikipedia gives an idea of just how many values are possible. It says that in order for the probability of a single duplicate GUID value to reach 50%, then every person on earth would need to own 600 million GUIDs. What this means for you is that you no longer have to worry about how to ensure that your identifiers are unique. Just generate a GUID value and use it. When you need another one, you don’t increment the first one. That’s not how GUIDs work. Instead, you generate a whole new GUID each time you need one. The only things you can meaningfully do with GUIDs are generate them and test them for equality. What I mean is that there’s no concept of one GUID being less than or greater than another GUID. They’re either equal or not. They’re not incremented like a simple number. And they don’t look like a number either. When you see a GUID displayed, it’s usually formatted as 32 hexadecimal numbers separated into 5 uneven groups with dashes between each group. Sometimes, the formatted value will appear inside curly braces, and sometimes inside quotation marks, sometimes any hexadecimal characters will be in uppercase and sometimes in lowercase. Just remember that this is just for display purposes. A GUID is still just 128 binary bits. Here’s what a GUID looks like when displayed: 65EA9162-DF2A-43BC-B691-7DFE4EF3EC63 Listen to the full episode for more on GUID or you can read the full transcript below. Transcript One simple way to keep track of things is to just use a number. Even a simple unsigned integer should give you over 4 billion numbers. Just start at 0 and increment each time you need a new one. You can even start at one just in case you want to reserve zero to mean something special. What can go wrong? Plenty. What do you do if you want to create objects from different threads? You could have a synchronized and thread safe identity factory that hands out new numbers for any thread that needs one. But then it becomes a source of contention as threads have to wait their turn. Maybe you let a thread ask for more than one identity number at a time. That could work. But let’s think about long term use. You’re going to want to save the state of your factory so that when the user exits the app and starts again later, then you don’t want the factory to start over at an id of one. It needs to remember which numbers have already been taken. What happens when the user upgrades versions and the saved identity numbers get mixed up with some older copy? Actually, this can happen anytime. If the user has a backup service, then it’s always possible that some files could get restored while others remain. This can cause your application to start issuing numbers that have already been used to identify other objects and now you can’t tell them apart. If this is a large customer, the damage could be severe. And even if you solve all these problems, you still have issues. What do you do when your application and some customer’s u

Visit the podcast's native language site