CodeSOD: Uniquier

This post was originally published on this site

The Daily WTF

Sole Purpose of Visit‘s team had a simple enough problem: they needed to generate unique labels for widgets on the screen. These widgets just need those unique IDs for internal purposes, so a simple incrementing counter would have been sufficient.

That isn’t the direction they went in.

namespace Initrode.Widget { /// <summary> /// Requirement: we need a string of length 22 for a widget id /// Note changed from static to instance to allow for TPL. /// </summary> /// <remarks> /// Was static. Changed to non-static class so that it works with Parallel.ForEach /// </remarks> public class Labeller { public string UniqueValue { get { StringBuilder sb = new StringBuilder(); byte[] bytes = Guid.NewGuid().ToByteArray(); sb.Append(Convert.ToBase64String(bytes)); // Make sure we don’t include characters that will interfere with later string output sb.Replace(‘/’, ‘&’); sb.Replace(‘+’, ‘#’); sb.Remove(22, 2); return sb.ToString(); } } } }

The initial comment lies: they don’t need 22

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