Letter the Sixth – The Nastiness of Namespaces

18Jun11

My Dear Malware,

It seems an age since I last wrote to you. Naturally, time passes differently for us, and I have been busy – going up and down in the world, spreading discord and hatred; the usual stuff. You would not believe how dusty some parts of the Middle East are! I swear I will never get the sand out from under my talons.

Now I am back at my desk, ready to dispense advice. My little excursion has drawn my attention to how very much in love our patients are with the concept of hierarchy. Of course, this has always been the case – the worlds great religions could not exist without it (and without our help). But it is amazing how eagerly humans will apply hierarchical solutions to even the simplest problems. And you should of course be hovering there, ready to suggest they vastly overdo it, making their code unreadable and unmaintainable.

Take for example the concept of a namespace. These humble creations were originally intended to solve a simple problem – two humans use the same name for a function in two separately developed libraries:

// library foo
int f();

// library bar
int f();

If some benighted human wanted to use both functions in his code he was in a quandary, and oh the delicious “workarounds” our Hellish architects whispered in human ears to “solve” that!

The solution was in fact obvious, though it took many long years to be implemented. The names of the functions needed to be decorated so that they could be explicitly referred to:

// library foo
namespace foo {
    int f();
}
// library bar
namespace bar {
    int f();
}

Now the humans can refer to

foo::f()

and

bar::f()

as required. This works well, and is what namespaces were intended for. I can tell you there was much groaning from the Throne Below when this was introduced into the C++ language, and when other languages rapidly followed suit.

However, the legions of Hell (and we are legion) are not easily daunted! We immediately fought back. Firstly, we convinced people not to use a term like “decorated” (which sounds entirely too much like something one would do that disgusting thing, a “Christmas tree”), but instead to use that much more suitable term – “mangled”. And then I (yes, modesty allows me to admit) had a wonderful idea. Why can we not take advantage of our patient’s mania for hierarchy? Can we not encourage them to write code like this:

namespace MyLibrary {
    namespace Useful {
        namespace Utility {
           int f();
        }
        namespace Business {
           int f();
           namespace Utility {
                int f();
           }
       }
    }
}

It turns out that almost no encouragement is needed. Humans apparently actually like writing stuff like this:

MyLibrary::Useful::Business::Utility::f();

and when they have done so seem to think they have done something profound. The thought of naming the functions so that their own names are distinct seems repellent to them. And even when the names are distinct, and there is absolutely no reason to use these ludicrous constructs, they will still embrace them as they would long-lost lovers.

This is all to the bad, but the masterpiece of this scheme did not, I am ashamed to say, come from us but from the C++ community. They replaced (with no prompting on our part) the perfectly workable, easily searchable idea of static functions with the concept of functions that are enclosed in a namespace without a name! Unfortunately, some of them seem to have seen the error of their ways regarding this delightful feature.

I have expounded at length on this because there are important lessons here for us tempters. We must always suggest that our a patients over-architect everything, that they use a class when a free function would suffice, and a class hierarchy when a single class would do. And all of these classes and functions must be wrapped up in deeply nested hierarchies. If we can get them thinking like this (and by and large we have already done so) our job is half done.

I will close now, not least because my talons are killing me! Between the sand and the pain of prodding at this iPad (admittedly a great coup for us, but does that mean we have to use the wretched things ourselves?) they are near to cracking. If anyone is looking for me, I will be in “Talons & Tans” on Wood Green High St.

Your loving uncle,

PUNCHTAPE
Under Consultant
Demonic Department of Obfuscation and Standardisation

Advertisements


One Response to “Letter the Sixth – The Nastiness of Namespaces”

  1. For added points get your mortals to use an anonymous namespace in a header file.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: