A blog about in-depth analysis and understanding of programming, with a focus on C++.

Friday, November 03, 2006

C++ and Taking it to the Limt

So, what's the point of this Blog?

I'm a C++ programmer, working in the programming industry. Programmers are a pretty intelligent lot. Programming is a meritocracy; if you can't hack it, you just don't get in.

And yet, you cannot find more conservatism among programmers than you do among C++ programmers. Nobody wants to touch templates at all. Or, if they do, it's only in the most basic, simplistic way as in STL containers (vector, list) and nothing more than that.

You don't get that with other languages. You don't see C# programmers categorically avoiding delegate functions and event handlers. You don't see Java programmers flee at the very mention of serialization or other such things. Lua programmers don't shiver in fear of using coroutines, meta-tables, or other language features. They look on these as just tools, to be used where appropriate and to not be forced into places when not.

Why do you get this attitude with C++? Well, it's simple really. You hear horror stories (and I've experienced some of them) of some programmer having to deal with some incredibly obtuse piece of C++ code. You know the kind; the code that uses templates far more than is needed. The kind where following the code at all is virtually impossible, even in a debugger. And heaven forbid that you have to actually debug through any of it.

It has been long said that you can shoot yourself in the foot with C, but C++ gives you the ability to take off your whole leg. So the conservatism is understood.

But more than anything, it is an anathema to programming. C++ is capable of so much, yet so many C++ programmers use C++ as though it were merely C with language support for OOP.

There are a lot of alternatives to C++ these days. C#, Java, even scripting languages like Python and Lua are considered alternatives to C++ in some circles. The primary advantage of C++ over those languages is raw speed. Yet so many programmers look at C++ for performance and believe that it just doesn't matter. That the ease-of-use of other languages outweighs these concerns. And if you need performant code, you should use straight C, because it is more standardized (in terms of compiled binaries).

I understand that point of view, but I disagree with it. C++ can get most of that ease-of-use, but not if you pretend that it's just C with objects.

For example, did you know that you can write functional programming in C++? I don't mean something kinda like functional programming; I mean just about the whole thing. Taking functions and applying functions to them to get new composite functions. You can get closures, but you'll sacrifice the look of your code. You need to download a header library to do all of this, but that's all.

You can write a parser in C++, directly translating an Enhanced Backus-Naur Form context-free grammar into C++ instructions. You can bind functions to a scripting language without any code generation; purely through the C++ template processor. You can write C++ code that is virtually guaranteed never to leak memory; who needs slow nonsense like garbage collection in the face of that?

But in order to do any of this, you have to do something that far too few conservative programmers are unwilling to do: use all of the languages features. No longer will we fear templates. No longer will we fear exception handling. No longer.

The purpose of this Blog is to teach you how to take C++ to the very limit. To the point where it stops looking like C with objects, and fully becomes the possibilities of what C++ can really do.

Undoubtedly, some of these features will frighten you. But remember back to when pointers frightened you? That didn't stop you from becoming comfortable with them (and if it did... well, did you come here by mistake?). If you don't want to be a programmer, don't code. If you're ready to take C++ to the Limit, then screw your courage to the sticking place and follow me.

No comments: