Data Structure IDs

By | May 5, 2023

Several times in my career, I’ve needed to deal with data structures that lasted more than a short period of time. Sometimes they were actual objects, sometimes just collections of data that belong together without any actual behavior.

Many times these structures needed to be stored somewhere. When working on projects like that, I find that someone usually adds an ID to use for lookup. There’s a pattern that seems useful.

ID

There should always be a permanent, unique id. Importantly, it should not be used for anything but identifying this chunk of data. Every time I’ve seen one used for more, it’s been a mistake.

Making structured IDs tends not to be as useful as you might think. (Grouping IDs by a common prefix or suffix.)

This is only used for lookup.

DisplayName

The first thing misuse of the ID is to make it something that is displayed to a user as something like a name. There are two points where this falls down. One is that display names for data may not be unique across time. The second is that users expect to be able to rename things.

It’s much better to have a separate field for display. It will only be used to display to the user.

Type

At some point, you will need to make decisions based on the data. Then, on groups of similar data. You may start off by collecting together groups of IDs (which turns into a maintenance nightmare).

As mentioned above structured IDs tend not to work, because you will eventually need to move a chunk of data to a different group and now you have to change the permanent ID. It’s better to have a separate type field or even a group of tags.

This field will only be used for making decisions about the structure (including decisions about a group of them).

Version

If this structure will be stored, or sent to another program, or live for any length of time, it will need a version field. No matter how good your design, it will eventually change. Having a version field means that you can tell which design you are working with quickly. I have seen people try to look for the existence of fields or type of data to try to guess the version, when a simple field would have been easier.

Leave a Reply

Your email address will not be published. Required fields are marked *