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:
- Why I believe C++ libraries are often complicated.
- 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.
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
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.
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.
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!
>>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…
@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
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.
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
}
}
Hi Bob E.
If you want to try Smalltalk
Download Pharo at http://www.pharo-project.org
or Squeak at http://www.squeak.org
have fun
Regards
Ted
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.
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!
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.
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++.
“top C++”, if anything, is all the high quality libraries yet to be written for it. This is even more true for C++11.
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.
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 …
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!
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.
01000101 01110111 01100001 01100011 01110100 01101100 01111001
01010000 01100001 01110101 01101100
01101111 01110111 01101111 01101111 01110000 01110011 00100001 00001010
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.
OS X is NOT written in C++. It is a mixture of Objective-C and C. Another thing with no C++: Linux.
John, it has been a while, but if you find a link to the Top C++ article, please share. Sounds intriguing.
Shameless plug — although I have no vested interest, just a fanboy — D is a systems language, and has an opt-in subset called Safe-D that is intended for applications programming. Seems a bit like Bottom C++ and Top C++.
I have a love-hate relationship with C++. Despite all the complaints against it (my own grousing included!), it’s still a widespread and popular language. If you know it you can get a job using it. That’s a pretty good metric for being considered a successful programming language.
“C++ standard binds rivals”
by Elizabeth Heichler, San Francisco
Computerworld
Aug 28, 1995
Well, okay then! Serendipity. There you go John. :-)
Top C++ is harder to use than other pure high-level languages such as Java, that’s why people don’t use it for high-level programming.
Raw pointers have 1 million ways to cause memory leaks. Smart pointers try to improve on it but at the same time causes readability issues:
E.g.
#include
std::shared_ptr cat = std::make_shared(“Lucy”);
can be done in Java as:
import Cat from mynamespace;
Cat cat = new Cat(“Lucy”);
which is way easier to read and write.