Wednesday 20 April 2011

Code: Gibberish++

I love C++.

And I hate it.

Whenever I pick up my C++ compiler, it feels like I'm coming home. It's a wonderfully powerful language, and one that, in the hands of the experienced, can be used to craft exquisite code. It can also be made to construct a bloody mess. Literally.

It's a sharp tool. With sharp edges. Whenever you pick it up, you realise that you have a sharp tool already covered in the blood of previous unfortunate users.


The language ranges from the sublime to the ridiculous, through fields of the inexplicable, unusable, and unreadable.

A great example of the gibberish end of C++'s spectrum is this example given me be a colleague.

What does the following code print?:
using namespace std;
int x = 50;
cout << setw(8) << setfill('0') << showbase << hex; cout << x << endl;
Any guesses?

The answer is: 00000x32. Um, handy.

What you have to say is this:
using namespace std;
int x = 50;
cout << setw(8) << setfill('0') << internal << showbase << hex; cout << x << endl;
To get: 0x000032.

Iostreams. Gibberish++

1 comment:

Phil Nash said...

You could almost forgive iostreams for being so verbose, arcane, and messy - if it was the price you paid for performance.

Sadly performance is probably the worst aspect - and I don't believe that's entirely a QoI issue.