Home

The wrong way to criticize C++

Whenever it comes to a discussion about programming languages and I mention the keyword "C++", this simple utterance alone raises great revolt and opposition. I think, most of this results from a fundamentally wrong way of criticizing C++.

Introduction

The C programming language appears well accepted. Nobody complains when I mention its name. C was designed as a "high-level assembler", a language that lives just at the doorstep of the CPU and whose compiled code is as efficient as self-written assembler, just easier to use. And it achieved its goal. All people seem to love it, despite its considerable primitivity. Even if you want to implement just simple data structures such as a list, you will have to write a significant amount of low-level, procedural code. No problem -- that's the way C works and shall work.

But as soon as I say "C++", the vibe changes. All of a sudden there is a hailstorm of complaints. In the following, I will go into some of the "arguments" that I hear regularly.

Complaint 1: “C++ is awful”

Answer: What shall I say? This complaint is so unqualified that an answer is not worth the effort. The speaker apparently just wants to decry and noodge.

Score: TIE.

Complaint 2: “C++ is bloated, it has too many features”

Answer: All in all, maybe. C++ is a big thing. There are only a few people who know it completely. But that's irrelevant. Did any C++ compiler ever make you take a test about the C++ language specification? No one has to know C++ in a whole. The fine details aren't needed in everyday's practice. You can write excellent C++ code without knowing all of it. The most important parts are no problem to learn. I could do it, everyone can do it. (Except those who cannot.)

Score: TIE.

Complaint 3: “C++ has multi-inheritance/feature X, and multi-inheritance/feature X baaaaad”

Answer: This complaint is pointless, since the mere possibility of multi-inheritance/feature X doesn't mean you have to use it. In fact, most C++ software doesn't use multi-inheritance. The multi-inheritance argument usually comes from Java apostles who wage a religious war of languages. (By the way, mediocre Java apostles, since they usually don't know that Java implements multi-inheritance through interfaces.) C++'s ability of multi-inheritance/feature X is a strength because in the rare cases where you need it you got it. That's all!

Score: C++ WINS.

Complaint 4: “C++ seduces you to write bad code”

Answer: Well, this argument is clever. It's not so easy to invalidate, since it's at least partially true. C++ is a multi-paradigm language, so it gives you very, very, very much freedom of choice how to write your code. If you're unexperienced or simply untalented, you don't know how to deal with this freedom and cobble your code together in a gruesome manner. Languages like Java give you a tiny bit more orientation. (But not much. If you are untalented, you write bad code in every language).

Score: CONTENDER WINS.

Complaint 5: “C++ code looks ugly”

Answer: Beauty is in the eye of the beholder. I know that C++ code is not necessarily the clearest code one can think of (Python looks clearer). But is it ugly? I know that C++ code can be very beautiful if you appreciate C++ and use it carefully and intelligently. Follow the guidelines of good coding style, and you won't have much problems with ugly code.

Score: TIE.

Complaint 6: “C++ implements multiple solutions for just one problem”

Answer: YES, it does. But this is just one of the strengths of C++: it doesn't dictate you how to solve a problem, it instead offers you several possibilities. You can choose the one that fits your needs best. Again: This is a strength, not a weakness. Of course, the programmer must be able to cope with it.

Score: C++ WINS.

Complaint 7: “C++ programs are slow”

Answer: This one is simple. No. Design a good architecture, write good code, make a benchmark, and experience the truth.

Score: C++ WINS.

Complaint 8: “C++ code is difficult to understand and maintain”

Answer: Oh my god. That's just ridiculous. C++ isn't lambda calculus! Just write good, clear code (yes, this is supported by C++, despite not being Python), then you can understand and maintain it.

Score: C++ WINS.

Complaint 9: “C++ is difficult, it takes long to master”

Answer: I know that C++ can be difficult for programming beginners. But that's ok. It is true that C++ is not the best first language, there are too many things to keep in mind. But this also holds for many other complex languages, e.g., Java. If you want to become a true C++ veteran, you will need many years of experience. This is not because C++ is difficult, but because it has many features (see complaint 2). But I don't think it is reasonable for everyone to become a high-profile veteran. Remember: nobody has to master all facets of C++. A subset is enough for a solid everyday's practice.

Score: TIE.

Complaint 10: “C++ is unsafe”

Answer: I can't see why C++ should be unsafe if you follow the guidelines of good coding style. Of course, C++ allows you to do unsafe things. This is because C++ doesn't want to restrict your possibilities. Again: This is a strength, not a weakness. If you don't need all the freedom C++ wants to grant you in your project or are just afraid of freedom, then C++ might indeed be the wrong language for your project. If developing for the .NET platform, maybe you should take a look at the managed C++ dialect C++/CLI.

Score: TIE.

Complaint 11: “C++ has no garbage collection”

Answer: It indirectly has. Look here.

Score: C++ WINS.

Result

Now, we have 5 ties, +1 for contender and +5 for C++. So, I want to declare C++ the winner. What a surprise!

Why are the above complaints invalid?

Well, you may think "but these complaints are reasonable". Think about it: they may be reasonable for you, but are they reasonable for everyone? All the above complaints just express personal opinions or attitudes.

For example, if some person X just likes very restrictive languages combined with extensive runtime checks because he finds that safety has top priority, he may bring complaint 10 ("C++ is unsafe"). What X doesn't realize is that every language, and so C++, has a certain focus. This focus may be unsuitable for X's project, but it is not necessarily "wrong" in general. And: just because X personally doesn't like permissive languages, he cannot validly say that all such languages are bad languages. Personal attitudes are just irrelevant -- the only things that count are experience and projects.

To make it short: Important decisions should base upon experience, not attitude or philosophy.

What are the real drawbacks of C++? Should I use C++?

Now the question arises if you should use C++ or not. The answer is simple. It depends on your project. No language is suited for all needs. C++'s strengths are freedom and speed. Of course, this has drawbacks. The main drawback of C++ is its low abstraction. On the one hand, it is truly object-oriented, but on the other hand, the technical details are not hidden from the programmer. This is also a strength, because it enables you to be object-oriented and fast, but it can hinder you. This is a situation where C++ might be the wrong choice for your project. Use C++ if you want to design a professional software architecture that is reflected by highly efficient code.

For the sake of completeness, I want to emphasize that we are talking about compiled code (why else should we talk about C++). If you don't want or don't need compiled code, then of course other languages than C++ might be better suited for your project.


© TR. Last modified: 11 Jun 2010