Why I Dislike C++
I’ve always been a big advocate of C. The simplicity of the language is elegant and beautiful. It’s clean, fast, and it’s everyplace. But “recently”, I’ve heard disturbing rumors of an upstart language attempting to capture C’s market share: C++. Having a reasonable amount of experience with C, and having taken a class on C++ at the University of Minnesota, I feel at least partially qualified to explain–in my usual cynical and aggressive way–why C++ is not conductive to streamlined program development. To open, I wish to quote Linus Torvalds, benevolent dictator and bigwig of Linux:
C++ is in that inconvenient spot where it doesn’t help make things simple enough to be truly usable for prototyping or simple GUI programming, and yet isn’t the lean system programming language that C is that actively encourages you to use simple and direct constructs.
C++ is a horrible language. It’s made more horrible by the fact that a lot of substandard programmers use it, to the point where it’s much much easier to generate total and utter crap with it.
Also I wish to quote Eric S. Raymond’s excellent The Art of Unix Programming:
…the language’s principal designer has conceded that he doesn’t expect any one programmer to grasp it all. Unix hackers do not react well to this; one anonymous but famous characterization is “C++: an octopus made by nailing extra legs onto a dog.”
- Performance – There’s persistent rumors that C++ programs are slower then C programs. This seems logical, seeing as how type polymorphism and other object oriented techniques are much harder–and in some places impossible–to optimize than simple procedural programs. This on its own is insufficient, however, as choosing a language purely based on performance is in many cases premature optimization. Therefore, I proceed.
- Beauty – C++ is, quite simply, ugly in my opinion. While the syntax of C can be learned in a few hours, the syntax of C++ can take days to fully understand. It’s filled with absurd and petty little rules, specifically in operator overloading, type polymorphism, inheritance, namespaces… in fact, let’s just say everything. This I imagine is debatable. However, as a student of the language who was already familiar with comparable languages such as C and Python, I found it to be a mess. Especially since many features of C++ are quite brittle and break if you don’t use OOP and only OOP. Try uses an enum with templates, and watch as your compiler starts yelling at you.
- Why? – This is a biggie. Quite simply, C++ fixes nothing. The big problem with C was memory management. Sure, you can boast about how automatic garbage collection is for wimps and whatnot (as I have), but pointers are one of the biggest sources of bugs in C. C++ did absolutely nothing to fix this. It’s like Subversion versus CVS (Watch Linus Torvald’s talk about git here); they missed the point.
- Debugging – I can easily imagine C++ programmers regularly having nightmares featuring compilation errors, and for good reason. While most languages like C and Python give fairly clear errors, a simple syntax blip in C++ can–and will–turn into a page of bizarre and non-readable errors that have absolutely nothing to do with the actual problem. To quote one of my TAs to the best of my ability (I took the class a while ago),
That’s why some people hate C++ with a passion.
- OOP – Firstly, let me point out that I do, in fact, like OOP. I use and love it in Python. But I don’t lay awake at nights thinking “Gee, I wish C had classes!”. Rather, I lay awake at night thinking “Gee, I wish C didn’t make me manage my memory manually!”. This is much like point three; the designers of C++ missed the point. But that’s not what this point is about. I wish to quote again Eric S. Raymond in The Art of Unix Programming:
They encourage architectures with thick glue and elaborate layers. This can be good when the problem domain is truly complex and demands a lot of abstraction, but it can backfire badly if coders end up doing simple things in complex ways just because they can. All OO languages show some tendency to suck programmers into the trap of excessive layering. Object frameworks and object browsers are not a substitute for good design or documentation, but they often get treated as one. Too many layers destroy transparency. It becomes too difficult to see down through them and mentally model what the code is actually doing. The Rules of Simplicity, Clarity, and Transparency gets violated wholesale, and the result is code full of obscure bugs and continuing maintenance problems.
This isn’t a problem necessarily with OOP per se, but it does make it easy to shoot yourself in your own foot, which C/C++ already make quite easy via memory management. This topic is very complex and obviously full of political landmines (since, unfortunately, in non-Unix environments, C++ has been offered as The Solution). I highly suggest reading The Art of Unix Programming if you have the time.
I wish to emphasize that C++ has its place. It’s not uniformly bad, and in some situations such as games and GUI development, it will probably make the developer’s life much easier. But the point of this article is to suggest that in many cases, it’s better to steer clear of it.
EDIT:
I should clarify a little bit. C++ was not hard for me to learn (though truth be told my class didn’t get too deep into templates and didn’t cover namespaces.) I just don’t think that the extra complexity inherent in the language is worth the gain.
Posted in Uncategorized | View Comments