CodeSOD: Ternt Up GUID

This post was originally published on this site

The Daily WTF

UUIDs and GUIDs aren’t as hard as dates, but boy, do they get hard, don’t they. Just look at how many times they come up. It’s hard to generate a value that’s guaranteed to be unique. And there’s a lot of ways to do it- depending on your needs, there are some UUIDs that can be sorted sequentially, some which can be fully random, some which rely on hash algorithms, and so on.

Of course, that means, for example, your UUIDs aren’t sequential. Even with time-based, they’re not in sequence. They’re just sortable.

Pitming S had a co-worker which wanted them to be sequential.

private String incrementGuid(String g) { if (String.IsNullOrWhiteSpace(g)) return “Error”; //else try { //take last char and increment String lastChar = g.Substring(g.Length – 1); Int32 nb = 0; if (Int32.TryParse(lastChar, out nb)) { ++nb; if (nb == 10) { return String.Format(“{0}a”, g.Substring(0, g.Length – 1));

To read the full article click on the 'post' link at the top.