I disagree with Linus Torvalds about C++

I heard about this note from Linus Torvalds from David Wolever yesterday. Here’s Torvald’s opinion of C++.

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. Quite frankly, even if the choice of C were to do *nothing* but keep the C++ programmers out, that in itself would be a huge reason to use C.

Well, I’m nowhere near as talented a programmer as Linus Torvalds, but I totally disagree with him. If it’s easy to generate crap in a relatively high-level and type-safe language like C++, then it must be child’s play to generate crap in C. It’s not fair to compare world-class C programmers like Torvalds and his peers to average C++ programmers. Either compare the best with the best or compare the average with the average. Comparing the best with the best isn’t very interesting. I imagine gurus like Bjarne Stroustrup and Herb Sutter can write C++ as skillfully as Linus Torvalds writes C, though that is an almost pointless comparison. Comparing average programmers in each language is more important, and I don’t believe C would come out on top in such a comparison.

Torvalds talks about “STL and Boost and other total and utter crap.” A great deal of thought has gone into the STL and to Boost by some very smart people over the course of several years. Their work has been reviewed by countless peers. A typical C or C++ programmer simply will not write anything more efficient or more robust than the methods in these libraries if they decide to roll their own.

Torvalds goes on to say

In other words, the only way to do good, efficient, and system-level and portable C++ ends up to limit yourself to all the things that are basically available in C.

I’ve had the opposite experience. I’d say that anyone wanting to write a large C program ends up reinventing large parts of C++ and doing it poorly. The features added to C to form C++ were added for good reasons. For example, once you’ve allocated and de-allocated C structs a few times, you realize it would be good to have functions to do this allocation and de-allocation. You basically end up re-inventing C++ constructors and destructors. But you end up with something totally sui generis. There’s no compiler support for the conventions you’ve created. No one can read about your home-grown constructors and destructors in a book. And you probably have not thought about as many contingencies as the members of the C++ standards committee have thought of.

I disagree that writing projects in C keeps out inferior C++ programmers who are too lazy to write C. One could as easily argue the opposite, that C is for programmers too lazy to learn C++. Neither argument is fair, but I think there is at least as much validity to the latter as there is to the former. I think there may be a sort of bimodal distribution of C programmer talent: some of the best and some of the worst programmers use C but for different reasons.

I do not claim that C++ is perfect, but I’ve never had any desire to go back to C after I moved to C++ many years ago. I’ll grant that I’m not writing my own operating system, but neither are the vast majority of programmers. For my work, C++ is as low-level as I care to go.

Related posts:

Conservation of complexity
Two perspectives on the design of C++
C++ templates may reduce memory footprint
Porting Visual C++ code to Linux/gcc

Tagged with: ,
Posted in Software development
119 comments on “I disagree with Linus Torvalds about C++
  1. FrankHB says:

    Linus Torvalds may be a specialist of Linux or git, but definitely NOT of C++(or even C). Did you know that he’d ever written some “C++ program”?
    C is badly designed for historical reasons, for example, the syntax for declarators and type names. Allowing vague linkage in ISO C(“tentative definition”) is just like illness, while C++ has ODR.
    If C++ throws the compatibility to C, it would be better for the language, technically. But political reasons work.
    There exist things badly design originally in C++, like syntax for new expression(hard to write debug macros) or templates(complex to parse notation). But compared to the whole language improvements, they are trivial.
    A modern C++ implementation is more powerful than pure C implementation. C++ could be fast as C in general, even faster for some critical contexts.
    C++ gives users more freedom. If you do not need something indeed, just not touch it, then everyone could be happy. Old code in a mess without enough documentation are equally evil, whether in C or C++.
    Another myth is “understanding”. I bet no one lives on this planet(certainly including Bjarne Stroustrup) could understand the WHOLE C++ language. To someone who is such romantic, I suggest having a look on ISO draft and defect reports.

  2. QuantDev says:

    @FrankHB


    Another myth is “understanding”. I bet no one lives on this planet(certainly including Bjarne Stroustrup) could understand the WHOLE C++ language.

    But honestly, you can’t at the same time claim that C++ design is sound and say that nobody on Earth understands entire C++. How can something which nobody fully understands be well-designed? Is C++ a gift from gods? If so, they have a cruel sense of humor.

    (I’m a C++ programmer, btw.)

  3. kiran says:

    One can only prove that a language is better than the other one is by way of writing software that is revolutionary. In this case linus has done it earlier but now it is your turn :) can you do it ???????

  4. QuantDev says:

    “One can only prove that a language is better than the other one is by way of writing software that is revolutionary.”

    That is false.

    ” In this case linus has done it earlier but now it is your turn”

    There’s been lots of great software written in C++.

  5. kiran says:

    Well!!!! I am looking for the greatest. Prove that an OS written in C++ beats and OS written partially in C and Assembly. Please

  6. M.S. Babaei says:

    @kiran That’s bcoz Torvalds C++/OO skills sucks and he did not know any better. Show us some C++ code written by Torvalds, and we show you how bad OO programmer is he.

    I’ve found this link on FreeBSD forums:
    24C3: Inside the Mac OS X Kernel
    http://www.youtube.com/watch?v=-7GMHB3Plc8

    I/O-Kit, the driver infrastructure written in C++ which is much much more effective than any C approach.

  7. kiran says:

    No…OO skills are meant for meant for being friendly to programmer and not to the system. I guess you got it already

  8. M.S. Babaei says:

    “OO skills are meant for meant for being friendly to programmer and not to the system.”
    You’re right on this one. But you don’t got what I meant in my post.
    He doesn’t know how to use C++, then why should he speaks about C++ and be so sure about his opinions?????

  9. FrankHB says:

    @QuantDev
    No one could fully understand C++, but there are more than just ONE. Those excellent committee guys have done good jobs to relax others. Most of us don’t (and have no need to) know as much as them, sometimes even inexperienced/childish. So not all criticisms are comprehensive/valid in fact, e.g. Linus’.
    C++ design is certainly not ideal. I’d also mentioned some nasty features(and I can give you more). However, (IMHO) the quality of design for a general purposed programing language is not tightly related with the complexity of a set of concrete features(though many shamefully unnecessary, which perhaps strongly attributed to C).
    C++ is extreamly complicated indeed, but in some sense, it deserves. C++ can do a lot more than C. It sounds.
    Again, no need to understand entirely(especially to merely found a solution/a language usage for a problem). Though you’d probably have more chances to overcome the traps if you know more:)

  10. QuantDev says:

    @kiran

    “Prove that an OS written in C++ beats and OS written partially in C and Assembly.”

    What does it mean “an OS beats an OS”? What criteria should one apply?

    @FrankHB

    “No one could fully understand C++, but there are more than just ONE. Those excellent committee guys have done good jobs to relax others”

    I’m sure you know the meaning of the phrase “design by committee”, right? ;-)

  11. FrankHB says:

    @QuantDev
    I know. It is not fit here even literally, for that WG21 only designed a few(e.g. covariant return type), while collecting and mending a lot.

  12. QuantDev says:

    @FrankHB

    They have not designed most of the individual features, true. But clearly they have designed the language as a whole, unless you’re saying that C++ simply has no overall design at all.

  13. M.S. Babaei says:

    from http://gcc.gnu.org/ :
    “GCC now uses C++ as its implementation language [2012-08-14]
    The cxx-conversion branch has been merged into trunk. This switches GCC’s implementation language from C to C++. Additionally, some data structures have been re-implemented in C++ (more details in the merge announcement). This work was contributed by Lawrence Crowl and Diego Novillo of Google.”

    from http://gcc.gnu.org/wiki/cxx-conversion#Rationale :
    “Migrating GCC to C++ as implementation language:
    C++ is a standardized, well known, popular language.
    C++ is nearly a superset of C90 used in GCC.
    The C subset of C++ is just as efficient as C.
    C++ supports cleaner code in several significant cases.
    C++ makes it easier to write and enforce cleaner interfaces.
    C++ never requires uglier code.
    C++ is not a panacea but it is an improvement.”

  14. FrankHB says:

    @QuantDev
    I think the overall design had been almost fixed long time ago. The standardization of C++ has a long history. Most of details, which Stroustrup’s D&E talked about, had been thought over even earlier. The committee have to gather ideas from users and figure out proper consensus(to be in the standard), but hardly change the overall design, which has been accepted by most core users(e.g. compiler vendors).

  15. QuantDev says:

    @FrankHB

    “I think the overall design had been almost fixed long time ago.”

    Really? Including templates, lambdas? What about concept maps? I find your assertion hard to believe.

  16. FrankHB says:

    @QuantDev
    Templates appear earlier than the first edition of the standard … at least half a decade?
    Direct support of closures/lambda expressions is an important feature, but I don’t think it to be “overall”. It is almost a grammar sugar, with few deep changes in the language infrastructure. It can play an important role in design of C++ programs, but not of the language.

  17. Craig says:

    It’s funny that you talk about comparing the best with the average, because that’s exactly what you’re doing here. Comparing the opinion of the best (Linus) with the average (yourself). Good for you for making the effort though.

  18. M.S. Babaei says:

    @Craig
    Do you even know what you’re talking about?? What made you think Torvalds is the best programmer out there??

  19. yyyy says:

    Craig, I don’t know linus is a best programmer or not, but I’m sure he is not good at C++, so his comments about C++ are unfair.About the words of linus, it is just like someone who are good at Chinese but can’t read English said that every novel write by English are craps.

    Some of you said C++ is slower than C, don’t just talk, prove it.I could swear that even linus don’t have the ability to do it with two reasons :
    1 : because it is false from the start
    2 : linus is good at C but suck at C++, how could a novice of C++ prove that C++ is
    suck?It is totally absurb