Letter the Second – Underscoring Human Frailty

01Dec09

My Dear Malware,

Thank you for your latest news. I agree that your bombarding of on-line programming sites with questions about “cascading style sheets” (whatever they may be) and “rounded corners” (as if anyone cared) will irritate and annoy a certain number (possibly even a large number) of programmers, but it seems a lot of effort to go to. As I suggested previously, the simplest approach is often the best, and to demonstrate this I would cite our introduction of the so-called “underscore” character into the programming world.


Of course, their really should be no such character. Even back in the days when mainframes and mini-computers ruled the land, the underscore did not produce an actual underscored character on anything but a paper-based Teletype terminal, and even then the effect was to half obscure the character it was underscoring. But our reasons for introducing the character into the ASCII character set were not simply that it wasted space that could have been better used by other glyphs. No; we saw in advance the horrible misuses that many programmers would make of it!

Consider this C++ header file, that implements a Date class:

// date.h
#ifndef __DATE__
#define __DATE__

class Date {
  public:
      // stuff
};

#endif

Notice that the conscientious programmer (poor fool!) has placed so -called “header guards” around the actual code to prevent problems with multiple inclusion of the header. This is good practice (and so something we try to prevent), but see what happens when he uses the header:

#include "date.h"
int main() {
  Date d;
  // more stuff
}

When he tries to compile this, the result with a popular compiler is:

main.cpp:3: error: 'Date' was not declared in this scope

This will puzzle and infuriate the programmer, hopefully for many minutes!

The reason of course is clearly spelled out on the C++ Standard. By the by, this is one reason we make standards documents so difficult to get hold of. All names that begin with or include a double underscore are reserved for the C++ compiler and standard library implementation. This means that the programmer is not allowed to #define such names in his own code. In the case of __DATE__, this is actually defined by all C++ implementations to be the date of compilation of a file, but any other similar names may be similarly defined by the compiler implementation.

As the name __DATE__ is already defined, the include guard test fails, and so the Date class declaration is never seen by the compiler, resulting in the “not declared” compilation error.

Our suffering programmer may be tempted to make this this change to his code:

#ifndef _DATE_
#define _DATE_

But we thought of that one long ago! Names that begin with an underscore followed by an upper-case character are also reserved for the C++ implementation, and the programmer may hopefully experience similar problems.

Of course, this prompts the question why programmers use the underscore to begin names at all. Why not simply say:

#ifndef DATE
#define DATE

If asked, they would probably give you the following reasons:

  • Microsoft (or Borland, or whoever) do it (but of course they do – Microsoft are compiler implementers – they are specifically allowed to do it)
  • I need to avoid clashes with other names (using underscores does nothing to avoid such clashes)
  • The code in my book does it (they never seem to consider that the book was written by an ignoramus or, as is often the case, by one of us)
  • Everyone else does it (ah, how humans love to be average)
  • It works for me (the last refuge of a fool)

So you see, dear Malware, something as simple as a humble underscore can cause difficulty and pain. And I have not even started on the problems associated with names beginning with an underscore and a lower-case character! I leave you with one final thought; mortals say the Number of the Beast is 666. In this they are quite wrong; it is in fact 95.

Your affectionate uncle,

PUNCHTAPE
Under Consultant
Demonic Department of Obfuscation and Standardisation

Advertisements


One Response to “Letter the Second – Underscoring Human Frailty”

  1. 1 Roger Pate

    Agreed. And how to use include guards correctly and easily: http://stackoverflow.com/questions/1744144/#1744302 (somehow appropriate to link back to SO since that’s where I found mention of your blog :P).


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: