Unholy characters in code

Just a head’s up — do try it at home — it’s most likely that your co-workers will not approve of these worst-practice naming standards…!

Prolog

It’s generally only when it comes to unit test method names that I allow myself to use underscores. Since those names tend to (and should) be a little chatty, it’s nice to separate the words to increase readability. That’s the first reason. The other reason is to be able to distinguish between Pascal-cased [variable] names in the sentence, and pure English words. For example; if you don’t use underscores it’s hard[er] to understand whether the name

means “Checking whether my name is empty” or “Checking whether MyName is empty”. Utilizing underscores between the actual words would clear that out.

Now to some funny business. For the faint hearted, please stop reading now!

Exotic characters

Well, underscores are nice enough, but you would go crazy having to read a whole book with them between each word, right? So, giving the fact that modern languages like C# now support foreign characters in addition to the English alphabet, and the fact that the number of charcters in total is rather large, maybe there are some obscure characters for us to use? And as luck would have it; there are. A couple, in fact.

First of all, of course not all unicode characters may be used in a name, just as little as e.g. a #-character would do. But there are a couple of characters that actually appears as spaces on screen, but are valid in type and method names. The character U+1160 (aka the Hangul Jungseong Filler) is one of those. Check this out:

Although (or since) this doesn’t generate any compiler errors, other developers stumbling across this piece of code might get pretty puzzled…

Please note: your source files will need to be saved in full unicode.

If you think that this is too hard to digest, and insist on having a character you can actually see, you might want to try these babies out:

Taking it even worse

If you really want to mess with your co-developers mind, you can even use a character with zero width (i.e. totally invisible). You will actually only notice it when you step it over using the arrow keys; then you’ll feel as the key press “didn’t take” since passing the character doesn’t move the caret.

You can for instance use these weird characters to make it appear as if you are “reusing” the same variable name many times by inserting one or more of these characters in the variable name. One of these invisible characters are U+200B (aka Zero Width Space). In the following example, the zero width space character is inserted 0, 1 and up to 4 times between “my” and “Value” in the different variable declarations. Please note that the code is perfectly legal and will declare five different variables (virtually appearing the same).

Quite nasty as they appear the same — but are in fact — totally different names… I guess the only way to make use of this character is in practical jokes…

The full monty

If you decide — against all common sense — that you really want to go for these unholy characters; you will find it pretty difficult since you’ll need to copy/paste them the whole time. Well, my friend, there’s a cure even for that. Just download and install The Microsoft Keyboard Layout Creator, and you’re able to assign one or more of these characters to physical keys of your keyboard.

Upon launch, go into the File menu and select “Load Existing Keyboard…”.

Load Existing Keyboard...

This will bring up a listbox with all the currently installed keyboard layouts on your machine. Select the one you are using.

Clicking the key-buttons on the main window brings up the small dialog where you can remap the selected key:

Press the “All…”-button to see the full dialog with all the modifier options (+shift/ctrl/alt etc):

Please be aware that there is a restriction of chaning the spacebar key to a non-space character. I tried to map the combination shift+ctrl+space to the “Hangul Jungseong Filler” (the invisible character) but was denied.


Maybe for the best! Tongue out I had to settle with ctrl+shift + the underscore key, which works good enough.

When you’re done with your keyboard tweaking, go into the Project menu and select “Build DLL and Setup Package”. This action will create a keyboard setup file which you can install on your PC.

After installing the package you can enjoy your new keyboard layout!

Wrapping it up

So, the real use of these exotic characters are maybe just to play some practical joke on someone. Or, one might construct an obfuscator utilizing these characters: imagine renaming all your names into different combinations of the Hangul Jungseong Filler and the Zero Width Space characters. Plain invisible code, my friend! Mohahahaa!

Author: Mattias

Senior .NET software consultant.

Leave a Reply