Saturday, April 7, 2007

C++ Mostly Sucks


Gentle reader,

If you have absolutely no interest in programming computers, you can safely skip this article. Rest assured, you will have lost nothing.

For the rest of you geeks...

I recently spent a couple of painful hours trying to rekindle my long-dormant interest in the C++ programming language. To give you an idea of my history with this language, many years ago I was part of a team proposing a switch from fortran77 to C++. I started learned the language back then and, as one does when one is learning a new language, I read books and wrote some small programs. Even back in the early 1990's, C++ was far more complicated than fortran77. After spending some time with the language I gave a talk to a group of Fortran users about the learning curve that people would encounter when making the switch from fortran to C++ and I estimated that it would take 6 months for people to learn C++. After the talk I was told by others who were more experienced than I was that my estimate was probably too short. In retrospect, they were absolutely right.

My main motivation for getting back into C++ was (and still is) a growing interest in game programming. It is difficult to avoid C++ because it has been used so extensively in that field. Well, I started to look at some game code and quickly remembered the reason I had abandoned C++ many years ago. Quite simply, the code looks like gobbledygook. Code in C++ is so full of artifacts (ampersands, asterisks, underscores, to name a few) that it just gets in the way of understanding the business logic.

I understand why C++ is widely used to implement low-level features like device drivers. It is a natural fit for such applications. However, coding extensive business logic in C++ is cruel and unusual punishment for yourself and for anyone who follows in your footsteps trying to understand your code. A programmer should be able to look at the code and quickly make sense of it. This requires clear variable names and uncluttered language constructs.

One area where clutter really gets in the way when reading C++ code is iterators. Iterators are clumsy, and looping over them is just plain ugly. Java used to have a similar problem, but that was solved with the introduction of a new language construct that I call the "for each bee in the beehive" construct. That construct makes code look more like English. Such code will be easier to write and, more importantly, easier to read.

As part of my effort to rekindle my interest in C++ I also picked up Bjarne Stroustrup's "Programming C++" 3rd edition. That book is a monster. It is enough to frighten away most computer science students because it is jam-packed with language details that are beyond what most "normal programmers" will care about. Apologies to Mr. Stroustrup but it is also not the most entertaining read. The feeling that I got from cracking this book and spending some quality time with it was "Goddamn it, life it just too short to be messing with this poxy language. I can be far more productive in Java (for big programs) or Python (for little ones)."

Sweet Jesus, spare me the details and let me get some frigging work done.

I realize that there is a ton of existing C++ code out there (much of it virtually intractable, pun intended) and there will be ample work for many years to come for those masochistic enough to thoroughly master C++. However, until all other programming jobs are dried up (I am guessing, never) I will continue to avoid C++ where possible when writing my applications.

Instead, I will write code in languages that are
a) easier to learn
b) more enjoyable to use
c) more readable
d) more productive

As for writing games? Well, it turns out that it's possible to write excellent games for most platforms using Java. And so for now, I am going to give that a try. I can keep myself amused, and hopefully, with the passing of time, C++, at least for business programming, will fade into oblivion. One can always hope.

On to today's quote. It's from the protagonist of C++ himself:

There are only two kinds of programming languages:
those people always bitch about and those nobody uses.
-Bjarne Stroustrup

To which comes the rejoinder:

Please create a programming language where there isn't quite so much to bitch about.
-Buford Twain

Until the next time, gentle reader, I remain,

Your friend,

Buford Twain

1 comment:

Randy Bryan said...

Great article! Thanks for submitting it to my blog carnival at:

http://techloaf.com/index.php?/archives/34-best-of-web-2.0-Blog-Carnival-April-23,-2007-Edition.html

I have a similar background in programming. This article really took me back in time. Do you know what language the ipod uses? They sell games for it on iTunes for .99 cents. I think a programmer could make a lot of money there.

Randy