By Default Different

How to pick a name

Sometimes, you need to register a name with your environment, like a named mutex to be shared across processes, a Win32 user-defined message or a file name for shared memory backing. Actually, we found out the hard way today that Objective C class names also fall into this category because they register themselves with the process-wide shared runtime by name.

It is very tempting to come up with nice names, like tc_shared_mem.bin for the file-backed shared memory. It makes debugging easy because when seeing the name you understand what it is. And although the name is global, it seems an awful coincidence if someone would use the same name, right? Until it happens.

Who is the most likely culprit to reuse the name? You! Your program is probably getting updated once in a while, and the new code is likely to contain some of the same names. If you are lucky, it does not matter. If you are unlucky (and we were in the case of Objective-C classes), havoc ensues.

Of course, you could start mixing the version number into the name to guard against such problems. And then hope that no one forgets to increment the version when recompiling - even during development.

But maybe the clash does not even require different versions. What if two instances of the program running at the same time already cause a name clash? Or a left-over temporary file is reused when the program restarts? Or the temp directory is misconfigured to be shared across users?

At think-cell, we use the philosophy of by default different. Names must be different unless they must be the same:

  • If the name is only shared within one instance of the program, the name is generated as a random string at runtime, when the program starts.
  • If shared memory is shared by different instances of the same program, you probably still do not want to ensure compatibility across different versions of the program. So its name is generated as a random string at compile time, and each compile run produces a distinct name.
  • And even then, do you really want the shared memory to be shared between different users sharing the same machine? Across different desktops? Maybe mixing the user or desktop ID into the name is actually correct.

In any case, making names different by default will force you to think about what level of sharing you really want and guards against surprises if the names unexpectedly meet in the same namespace.

— by Arno Schödl

Do you have feedback? Send us a message at !

Sign up for blog updates

Don't miss out on new posts! Sign up to receive a notification whenever we publish a new article.

Just submit your email address below. Be assured that we will not forward your email address to any third party.

Please refer to our privacy policy on how we protect your personal data.