The Problem With C++

MIT's Technology Review recently interviewed Bjarne Stroustrup in a two-part article (part one, part two). You may know Bjarne as the inventor of the C++ programming language. Indeed, he even maintains a comprehensive C++ FAQ that answers every imaginable C++ question.


This is a companion discussion topic for the original blog entry at: http://www.codinghorror.com/blog/2007/01/the-problem-with-c.html

Haha, you obviously have no idea how a compiler works and what the difference is between a compiler, an intepreter, a bytecode and a JIT compiler.

Anyway as someone way above stated very clearly C will always be used for low level stuff, kernel and drivers. C and not C++ which has nothing to do with C whatsoever. Furthermore if you want speed or small code you use C, not C++ and all the mess it generates with classes and stuff.

FYI ā€“ there is now post-tension concrete that actually makes concrete harder because it is compacted. also, there are different grades of concrete, the best using ash that strengthens concrete over time. it just goes to show the old saying is true: garbage in, garbage out.

C and C++ will always have their place for writing core libraries. and iā€™m a lot more comforatble managing my own memory (until i get Alzheimerā€™s) than some new-college grad thatā€™s never heard of pointers. Even MS developers donā€™t trust the garbage collection in the .NET languages. (i canā€™t find the post right nowā€¦)

ā€œBut when was the last time you used C++ to write a line of business app or website?ā€

Hmmm. I guess that would have been last Friday, January 12 2007, when I was writing code for flight software application at a major aerospace corporation. For some odd reason, we arenā€™t converting our insanely huge C/C++/Fortran codebase into Java, D, Ruby on Rails, Python, Perl, or whatever the current flavor-of-the-month language isā€¦

Iā€™ve been coding for about 20 years, in all sorts of languages. Assembler, Java, Smalltalk, JCL, REXX, C, C++, C#, Shell, Pascal, Perl, PHP, Ruby, TCL, Eiffel, Maple, Mathematica, Gaussā€¦god knows what else, even VHDLā€™s for FPGAā€™s. Let me say that C++ is pretty great, and infact is what Iā€™ve chosen to use for a number of new projects.

Here are some comments on assumptions on points above:

  1. Many assume ā€˜higher level languageā€™ needs to be ā€˜slowerā€™. Thatā€™s just not true at all. That said, if itā€™s slow in C++, itā€™s likely your algorithm, while if itā€™s slow in a high level language, itā€™s probably that youā€™re using convenience classes that are poorly suited for your problem domain.

  2. Itā€™s true that a lot of programmers arenā€™t formally trained anymore. If a programmer canā€™t work out time complexity between a Quick Sort and a Bubble Sort (really basic questions), I donā€™t hire them. Even if theyā€™ll be using a high level language. Iā€™ve seen hundreds of thousands of dollars go up in smoke because programmers honestly didnā€™t understand why the Java ArrayList behaved so poorly when used as a queue, and the queue size grew. Actually, change that: millions lost. High level langauges should be used for their expressive power, not as a crutch for people who shouldnā€™t be programming commercially. Itā€™s ilke the comment above, ā€œyou donā€™t know when youā€™re hitā€.

  3. Many high level languages are compromising in areas they shouldnā€™t, letting programmers be lazy. If thereā€™s one problem with coders in high level languages, is that they donā€™t seem to write much ā€˜error checkingā€™ code. If something like VB or C# had Design by Contract built in (like Eiffel has) forced as part of the language, Iā€™d be happier. I do know about Spec#, and other C# DBC extensions. Really, the language should be easy to use, but also do itā€™s bit to enforce some quality.

  4. Vistaā€™s way slow. I played with it yesterday and I could see delays in the icons on the start menu popping up. Itā€™s pretty pathetic. If I were Microsoft, I wouldnā€™t be proud of it.

  5. C# is a nice language. Iā€™d rather people learn Smalltalk tho. Squeak is cool.

Only in the last three years have I spent more time with C++, and Iā€™d have to say itā€™s a pretty excellent language. C# is cute too, but itā€™s performance isnā€™t there. I wrote a test system in C, C#, and Ruby the other day. Ruby got 11,000 operations/sec, C# got 34,000 operations/sec, and C++ got 71,000 operations/sec. The C++ code compiled on Windows, Linux, and BSD without any warnings or code changes. It also used the least RAM.

Fast? Portable? CPU and Memory efficient? Do these things matter to anyone?

If you canā€™t handle the power, just steer away from it. Donā€™t badmouth C++ because of your inability to use it correctly.

I donā€™t know, maybe itā€™s just that I donā€™t like to be kept in a little box that protects me from the real world, but I prefer using C++ over all these languages whenever possible. That said, I do think that some languages are more suitable for some tasks.

Citing Bjarne, ā€œIndeed, one serious problem is that currently, too many software developers are undereducated and undertrained.ā€ Just go to www.thedailywtf.com and youā€™ll see that most posts come from Java and VB ā€œprogrammersā€.

Myself, Iā€™m eagerly waiting for the new C++ standard that will give us more template power and some new library additions (among other cool stuff). Read about it at:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2122.htm

Iā€™m wondering why people seem to think they can optimize code better than a mature optimizing compilerā€¦ In general, itā€™s best to let the compiler do the work.

I also keep going back to the last point on the original post: which language best protects against human foibles? My answer would be the language that is the most readable and maintainable (good examples being Ada and Python). Almost all code will be read many more times than it will be written, and any long-lived system ends up with about 10% of the total cost in the development phase, and about 90% of the cost in the maintenance phase. Draw your own conclusionsā€¦

Thatā€™s exactly why I like C# so much. Itā€™s just too bad that we are restricted to one single platform by using it :frowning:

ā€œThe CLR will eventually destroy the class and force the connection closed, but you have no guarantee when the garbage collection on the object will actually happen.ā€

http://msdn.microsoft.com/msdnmag/issues/05/01/ASPNETPerformance/

One of my first Computer Science instructors told me that anyone who is a solid C++ programmer knows how to program and can pick up any language out there. This has held true since I heard it 10 years ago, with perhaps the only exception being Lisp (my favorite language to play with), which many OO programmers have issues with.

The reason C++ is a good language is because it makes better programmers of us all. From low-level understanding of how the machine works by figuring out how pointers work, to high level understanding of how objects and inheritance work, C++ covers it all. After spend 2 days trying to figure out why your program keeps saying ā€œSegmentation Fault (Core Dump)ā€, you learn/understand the importance of memory/pointer management. Struggling to write a linked list, and then learning about the STL makes you realize how important reusable code is.

Sure C++ is very difficult to learn and understand, but once you do, you have top to bottom knowledge and understanding of how a computer works. For all its faults, C++ should be a language every programmer has in his/her toolkit.

My $.02

C++ is difficult and for experienced programmers, experience at least with C. You can do whatever you want fast. But it is not for everyone.

Daniel: You can code cross platform with C#, Mono has come on leaps and bounds. You do have to put the extra effort of making sure your code is fully compatible with Mono and that you donā€™t use anything windows specific. You donā€™t get anything for free :wink:

Jeff: I whole-heartedly agree! C++ is a great language for hardware interaction or even for making a component that has a need for the most performance possible, but its power and flexibility means you have to take more control and responsibility over everything. More recent high level languages like C# take some of that power and flexibility away, but with less under your responsibility to directly control your productivity is greatly increased and the language/framework can protect you from many common mistakes.

Strangely, in my feedreader (Newshutch) the whole article references C and not C++ā€¦

There is another (than MS) C# compiler in existence:

http://www.mono-project.com/CSharp_Compiler

But alas, whatever the language, there will always be cases of ā€œ(accidental) use of the ordnance to hurt yourselfā€.
Some languages protect you more, many protect you less, but incorrect code is always incorrect code.

thereā€™s a new language out there released just a couple of weeks ago called ā€œDā€. It offers performance and flexibility of C++ and high level features like garbage collector. Syntax is very similar to java and c#.

a href="http://en.wikipedia.org/wiki/D_programming_language"http://en.wikipedia.org/wiki/D_programming_language/a

Is it just me or has the bitchiness level on this board has gone up considerably in the new year? Jeff writes an entry on Vistaā€™s search feature and everybody and his brother jumps down his throat for being pro-ms.
He runs a post about C++ and itā€™s relationship to newer languages and all the C++ people flip out and kick in condescending overdrive.

ā€œProgrammer lazinessā€ is the dumbest phrase I hear on posts these days. The fact is you can get considerably more done in a given amount of time with the ā€œcuteā€ languages like C# and Java than you can with C++ or C. Itā€™s easier to recruit or train people in them to maintain the code. If you are writing an application that lends itself to such a language/platform wtf would you not use it?
I donā€™t suppose any of you non-lazy types would bother to go to Ikea when you need a new coffee table. I bet you head out to the woods to chop down some trees and assemble it yourself. Damn youā€™re cool. Iā€™ll just sit down with my lazy-self, kick my feet up on my new coffee table and drink a beer while you ā€œrealā€ programmers are off in the woods fighting with bears and chopping down trees (no chainsaws, just axes).
Of course you wouldnā€™t rewrite all your legacy apps in a new language that makes no sense. Writing a whole new business application in C++ when C# or Java would do just as well? Well thatā€™s just a waste of time and money.

So C++ was a step in the wrong direction if you want a general-purpose programming language. For most tasks itsā€™ complexity is harmful.

Still, new mainstream-languages like Java and C# adopt the C and C++ā€™ syntax, the type system, and control structures. Maybe it is time to realize that all languages that stems from C are broken?

ā€œOne of my first Computer Science instructors told me that anyone who is a solid C++ programmer knows how to program and can pick up any language out there. This has held true since I heard it 10 years ago, with perhaps the only exception being Lisp (my favorite language to play with), which many OO programmers have issues with.ā€ --Bill

Iā€™m glad you mentioned this. Maybe itā€™s just me but I donā€™t sense that Bjarne values productivity. If you can do C++, youā€™re going to be able to spit out an web application using Ruby on Rails or ColdFusion in no time. But if you try doing it in C++, chances are one of these non-highly-trained-programmers are probably going to crank something out before the master C++ person. Itā€™s almost like heā€™s arguing an experienced plumber should keep welding copper pipes together instead of using those fancy sleeves where they can just quickly crimp together 2 pipes. Itā€™s important to know the basics but donā€™t run away from productivity lest you want to be out of work and out of business.

hotdogs: D-1.0 was released couple of weeks ago. D itself lives for dozens of weeks now.

TheGeoff, so you donā€™t agree that there is an increasing number of undereducated programmers, and that this is caused mainly because they are getting accustomed to have important tasks done for them? Read what Bjarne said in the quoted snippet, itā€™s all true.

Like Bill Watts said, competent C++ developers can be competent in other languages quite easily, while itā€™s not true the other way around.

Iā€™m not saying I hate languages like C# (but yeah, I truly dislike Java and VB) but Iā€™d say itā€™s healthy to remain competent in languages like C++. It actually seems they put time to design C#. But if youā€™re into the Managed stuff, Iā€™d recommend getting into C++/CLI.