Why do C++ folks make things so complicated?

This morning Miroslav Bajtoš asked “Why do C++ folks make things so complicated?” in response to my article on regular expressions in C++. Other people asked similar questions yesterday.

My response has two parts:

  1. Why I believe C++ libraries are often complicated.
  2. Why I don’t think it has to be that way.

Why would someone be using C++ in the first place? Most likely because they need performance or fine-grained control that they cannot get somewhere else. A Ruby programmer, for example, can make a design decision that makes code 10% slower but much easier to use. “Hey, if you want the best performance possible, why are you using Ruby? Didn’t you come here because you wanted convenience?” But the C++ programmer can’t say that. It’s not turtles all the way down. Often C++ is the last human-generated language in a technology stack before you hit metal.

This weekend The Register quoted Herb Sutter saying “The world is built on C++.” The article goes on to mention some of the foundational software written in C++.

Apple’s Mac OS X, Adobe Illustrator, Facebook, Google’s Chrome browser, the Apache MapReduce clustered data-processing architecture, Microsoft Windows 7 and Internet Explorer, Firefox, and MySQL — to name just a handful — are written in part or in their entirety with C++.

Certainly there is a lot of software implemented in higher-level languages, but those high-level languages are almost always implemented in C or C++. When there’s no lower-level language to appeal to, you have to offer a lot of options, even if 90% of users won’t need those options.

On the other hand, that doesn’t mean all C++ libraries have to be complicated. The argument above says that the lowest layers have to be complicated and they’re written in C++.  But why couldn’t the next layer up also be written in C++?

Some time in the 90’s I ran across an article called “Top C++.” I’ve tried unsuccessfully since then to find a copy of  it. As I recall, the article proposed dividing C++ conceptually into two languages: Top C++ and Bottom C++. Explicit memory management, for example, would be in Bottom C++. Top C++ would be a higher-level language. You could, for example, tell a compiler that you intend to write Top C++ and it could warn you if you use features designated as Bottom C++.

Of course you could slip from Top C++ into Bottom C++ if you really needed to, and that’s the beauty of using one language for high-level and low-level code. Application code in C++ would use Top C++ features by default, but could deliberately drop down a level when necessary. A section of Bottom C++ could be marked with comments or compiler pragmas and justified in a code review. Instead of having to cross a language barrier, you simply cross a convention barrier.

I thought this was a great idea. I’ve followed this approach throughout my career, writing high-level C++ on top of low-level C++ libraries. To some extent I put on my Bottom C++ hat when I’m writing library code and my Top C++ hat when I’m writing applications.

But most of the world has decided that’s not the way to go. If you’re writing C++, it might as well be Bottom C++. Instead of Top C++, write VB or some other higher-level language. There’s very little interest in high-level C++.

I imagine this decision has more to do with management than technology. It’s easier to have people either write C++ or not. If you’re writing C++, then use any language feature any time. I suppose this is a stable equilibrium and that the Top/Bottom C++ partition is not.

Related posts:

Software architecture as a function of trust
Programming language subsets

127 thoughts on “Why do C++ folks make things so complicated?

  1. (@ Peter: i didn write anything about Pascal, Paul did :o)
    @SeattleC++:

    I don’t hate C++ or any other programming language. Why should i? Most of these IT tools are made with years of hard work and effort, so one should respect the makers/inventors of these tools and see these tools in the concept of time when they were created. I know as well that C++ (or C) is still the base of a lot of software. One of my systems OS is Debian Linux, most of it is written in C or C++. Before C++. All very nice and functioning very well. I’ve spend many years with several programming languages (see my resume , written in Pharo Smalltalk and Seaside.st) and use whichever language the customers prefer (and am I in control of) But when writing (end-user) applications, I want little involvement with the underlying system and as much of these nitty gritty details being solved by the interpreter (jikes! an interpreter, isn’t that slow, you might think…no mind, it sort of “precompiles” each method when saving it). This way i can develop faster. That’s why I prefer Smalltalk. Of course Smalltalk has a learning curve. But i think it takes a lot longer to learn C++. Compared to Smalltalk, C and C++ are “closer to the machine” and, from the perspective of an “application” programmer, like I am. Thanks, for reading.. I will not bring this under your intention again :o)
    links: seaside.st, pharo-project.org, squeak.org.

  2. C++ programmers think different. No matter how close to the metal you want to think, you’re still a few abstraction layers higher than C++ programmers. Hence, what’s simple for them is complicated for you. C++ libs that are complicated for you are simple for C++ programmers.

  3. Peter da Silva — “1978” … check out the modern things that have happened :)
    Jordi Steffen — “(all C* are NOT comfortalble – too much special characters” that’s what attracts me to more humanly elegant things like Object Pascal.
    If you write direct Object Pascal you can get size right down, and speed is right up there.

    Paul

  4. Paul Norman
    I started with Wirths Pascal (I am from Zürich!), then later on Borland, and now I’m a fan of FreePascal. Also some OO but dont like this too much….

  5. Early in C++’s existence, C++ took a lot of crap from C programmers regarding performance. In those days, C programmers complained of the “overhead” of virtual function calls… and the “bloat” of objects.. As a result, the C++ community became super focused on performance… Most of the features in C++ are completely opt in (I.E, if virtual functions are too expensive, you are free to not use them and not pay any penalty)… This attitude bled into the STL…

    Another factor that complicates the STL is it’s approach to generic programming… In my opinion C++ took the purists approach (instead of the pragmatic one)… So, std::list doesn’t have a .sort() method… Instead, you can use the sort algorithm on any container that implements the right kind of iterators…

    IMHO, C++ needs 2 standard libraries… One for purists, and one for pragmatists… (and the pragmatists will use the STL to implement their standard library, but the pragmatist::list will definitely just have a .sort() method!)…

  6. Jordi Steffen “Also some OO but dont like this too much….”
    yes I avoid it and only use when necessary — on a usefulness basis rather than as an object ;) in its self .

    I appreciate that there are C++ people who find that their forte–the way they mentally operate and I respect that.

    What I think would be of greater help, would be for easier reuse of C++ libraries, some mechanism to make it easier for other languages to re-utilise the code developed there.

    Headers and .so and/or .dll are a real hassle. C can be brought into FreePascal more directly, but C++ is a dog, you have to write interfaces that inevitably means having to actually cope with all the things that stopped you wanting to have to deal with C or C++ any way!

    Paul

  7. I agree that C++ is complicated and needs a bit more braincells than Java.
    I first was a Java-guy and then switched to C++ and Qt. I don’t regret it.
    I don’t use any of the more complicated C++ features though. Mostly not worth the hassle.

  8. Paul: I spent over ten years following Wirth down the rabbit hole, off and on, and the only Wirth-style language I’ve used that didn’t have the same compatibility problems was Modula 3… and that’s only because it only ever had one real implementation, and _that_ was only portable because it used C as a back-end.

  9. Peter, Yes I benefited by coming in on the dialect family, on a branch that had been much improved by Borland over a decade (by Anders Hejlsberg the man who later developed C# etc at Microsoft), then picked up by FreePascal and Lazarus teams and helped along further, so I might have missed aspects of the Rabbit Hole that you found yourself moving through.

    mgoetz : “I don’t use any of the more complicated C++ features though. Mostly not worth the hassle.” – problem is if you get involved in an team situations or OpenSource project eventually someone does!

  10. >>In my opinion C++ took the purists approach (instead of the pragmatic
    >>one)… So, std::list doesn’t have a .sort() method…

    Actually it does…

  11. @TedvG Have never tried smalltalk but certainly sounds interesting. I would like to hear more about how using smalltalk (a “pure” oop) instead of c++ will cut productivity time by 25%? Either I just need to learn smalltalk or you need to understand c++ better. I don’t think c++ was designed with the idea of reducing verbosity strait out of the box. But if you take the time to do proper c++ development then eventually you’ll be writing up routines quicker than Ruby or Python developers. d

  12. No, no, you misunderstood. Smalltalk reduces the time during which you are productive by 15%. When you are using smalltalk, you have to spend time figuring out the meaning of phrases like, “send the message ‘+’ to the ‘Integer’ object.” WTF? One thing pure about smalltalk is pure silliness. And if you’re an OOP-hater, chances are you will hate smalltalk more than any other OO language. I could send a long write message to the book object about all the stuff about smalltalk I didn’t care for, but I think I’ll just walk away.

  13. John Wellbelove 01.03.12 at 04:42

    >>In my opinion C++ took the purists approach (instead of the pragmatic
    >>one)… So, std::list doesn’t have a .sort() method…
    =========================================
    Dear John,

    Please let me correct you , in std , there is a member function named sort() in list class , detail as below:

    void sort()
    { // order sequence, using operator<
    if (2 _Mysize)
    { // worth sorting, do it
    const size_t _MAXBINS = 25;
    _Myt _Templist(this->_Alval), _Binlist[_MAXBINS + 1];
    size_t _Maxbin = 0;

    while (!empty())
    { // sort another element, using bins
    _Templist._Splice_same(_Templist.begin(), *this, begin(),
    ++begin(), 1);

    size_t _Bin;
    for (_Bin = 0; _Bin < _Maxbin && !_Binlist[_Bin].empty();
    ++_Bin)
    { // merge into ever larger bins
    _Binlist[_Bin].merge(_Templist);
    _Binlist[_Bin].swap(_Templist);
    }

    if (_Bin == _MAXBINS)
    _Binlist[_Bin - 1].merge(_Templist);
    else
    { // spill to new bin, while they last
    _Binlist[_Bin].swap(_Templist);
    if (_Bin == _Maxbin)
    ++_Maxbin;
    }
    }

    for (size_t _Bin = 1; _Bin < _Maxbin; ++_Bin)
    _Binlist[_Bin].merge(_Binlist[_Bin - 1]); // merge up
    splice(begin(), _Binlist[_Maxbin - 1]); // result in last bin
    }
    }

  14. I can develop much faster by using Python with C extensions for performance critical parts. It doesn’t make sense to use C++ when I have best of two worlds – Simplicity and Easy-of-use of Python + Performance and Power of C.

    Python: wake-up C
    C: what can I do for you, master?!
    Python: calculate Inverse of Matrix 4×4 and set of quaternion rotations, I’m too slow.
    C: considered done.

  15. The title of this post is trivializing and tendentious and has nothing to do with C++.
    If someone thinks a piece of code is complicated the reason was that either the reader or the writer had a bad day or they don’t want to understand each other.

    There are people who restlessly invent or search for their optimal language. The reason is simple: They don’t want to understand C++.

    Why I ever will stick with C++: Open standard, durable, widespread, no limitations, performance, proven.

    Learn it – it’s worth the effort!

  16. You do realize top C++ has been around for a long time already dont you?
    its Called C#… “or middle C++ i would call Java”

    silly article.

  17. I’ve written Java and C#, but I wouldn’t call either of them “top C++.” The point of Top C++ is that it’s a higher level language, and it’s still C++, not just something with similar syntax C++ but something that is C++ so that there’s zero barrier to calling lower level C++.

  18. “top C++”, if anything, is all the high quality libraries yet to be written for it. This is even more true for C++11.

  19. I spent around 5 years on and off coding in C++ and then gave up on it due to the complexity of the language. I have now spent around 4 years coding in C on and off, as well as PHP. I’ve recently been set with a programming task for a job requiring me to implement code to read data from a file and provide sorted formatted output. The PHP code was easy, but just try getting something close to an associative array in C++ when you’ve not coded in it for 4 years! I could do it in C without any problems what so ever but I’ve been encouraged to use the STL. C++ may be a superset of C, but the programming paradigms and the anti-C propaganda that I’ve witnessed on many programming q&a sites really pushes one away from doing things “the C way”. So I would never lump C with C++ they are two very different languages despite some minimal common ground.

  20. C++ is not a complicated language … C++ folks are.

    Performance? Very few times (or never) performance parameters are specified … the reason? there is no theorical foundation fpr peformance people really follow.

    For instance, when people say: this is a real time system, they think in terms of seconds or milliseconds … and real time has nothing to do with clock cicles but with “business related actor response actions” which are the actions an actor can perform between 2 consecutive events in the target system … these actions could span millisecons, seconds, days, weeks, months and even years (if you are developing a monitoring system that spans decades).

    … and I think this missinterpretation of real time comes from the fact that we often think of actors as humans.

    So again … it is not the language … it is the people using it …

  21. My least favorite thing is confusing/super truncated variable names. Do c (and especially Verilog/VHDL) guys really type so slow that adding a few extra characters to some variable that’s used tens of times really adds up to any appreciable amount of time? I can imagine someone saying “But it makes the source code smaller!” HAHAH!

  22. Wow, I’d forgotten about this thread.

    John: “C++ isn’t the lowest possible level of abstraction, but it is the lowest level that many people consider.”

    That’s what I said, most people have no idea of what “low level” is.

  23. So to all the people saying top C++ is C#, Java, or some other language, when call I compile C# to my Android and iOS phone without paying a 3rd party an expensive licensing fee and being restricted to their tools? When will I be able to run my Java code on iOS? Maybe your arguments work if you don’t care about portability and reuse of code.

    Let’s see:

    Write a well-tested C++ library ONCE, with minimal porting and testing effort per platform.

    Versus

    Write Objective C for iOS, Java for Android, C# for Windows Phone, C++ for BB10, hmm… seems like a LOT more potential for bugs and a lot more time spent on dev here.

  24. OS X is NOT written in C++. It is a mixture of Objective-C and C. Another thing with no C++: Linux.

Comments are closed.