Letter the First – The End Of All Things

01Dec09

My Dear Malware,

I am in receipt of your missive complaining of the difficulties you are experiencing in creating new spiritual torments for programmers. May I suggest that you are approaching the problem from the wrong angle. Your idea of creating a new interprocess communication mechanism is good in itself – one can never have too many of these hellish things, and certainly COM and CORBA have caused more misery than most of our other infernal inventions (I except always Unicode), but have you considered something much more simple? For instance, the use of misleading names for functions can be a source of much pain to programmers, and is therefore to be encouraged.

Take for example the simple eof() member function for C++ istream classes. Almost all novice C++ programmers seem to misuse this function (if posts on Stack Overflow and Usenet, two more of our better ideas, are to go by). The code you mostly see posted looks something like this:

while( ! cin.eof() ) {
  int x;
  cin >>  x;
  cout << "you entered " << x << endl;
}

Now to you and me, this code is obviously wrong, and indeed would be to humans if they conducted a simple edge-condition test – give the code an empty stream to work on. The correct result for this test is to produce no output at all, but in fact what gets printed out is: you entered 14789 where 14798 is some garbage value. The problem for humans is that they will persist in reading the the eof() function as meaning “test if the next read will encounter an end of file” when of course it means no such thing! The function should really be called something like LastReadMetEndOfFile, but thanks to our fiendish representatives on the C++ Standards committee, we were able to force the adoption of the more “convenient” name of eof. The correct use of the function is then more obvious:

while(1) {
  int x;
  cin >> x;
  if ( cin.eof() ) {
    break;
  }
  cout << "you entered " << x << endl;
}

You will see code like this in cases where the programmer has realised what eof actually does. However, thanks to another of our cunning ideas, they have written more code than they needed to, and the code still fails if the user enters something that is not a valid integer. The cunning idea I speak of is the result of evaluating the expression:

cin >> x

Many novice programmers know that this returns the stream – after all, this is what allows them to read multiple values in the same statement. What they never seem to realise (thanks partly to our own humble efforts) is that this returned stream can be tested in a while() or an if() statement. Doing this produces the simple, clear and correct code:

int x;
while( cin  >> x ) {
  cout << "you entered " << x << endl;
}

The same holds for most other C++ input functions. I have no doubt you have often chortled over the needless effort that some programmers put into reading a line of text (I certainly have) when all they needed was something like this:

string line;
while( getline( cin, line ) ) {
  cout << "line: " << line << endl;
}

So you see, there is no need to create massively complicated architectures to cause pain and unhappiness to mortals – a simple application of meaningless names and the hiding of useful information can accomplish just as much. I look forward to hearing of your application of these ideas in the world of men.

Your affectionate uncle,

PUNCHTAPE
Under Consultant
Demonic Department of Obfuscation and Standardisation

About these ads


4 Responses to “Letter the First – The End Of All Things”

  1. 1 Nick Gorski

    I’m going to enjoy this blog quite a bit. :) I like the format.

  2. 2 Will Gorman

    Yes, I can’t wait for more articles. This is easily the best C++-programming-advice-in-the-style-of-a-C-S-Lewis-novel blog that I’ve read this year.

  3. 3 meekrosoft

    Good stuff, keep it coming!

  4. Excellent.


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


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: