Wednesday, December 17, 2008

Linus on C++

Reading this was pure joy!

"C++ leads to really really bad design choices. You invariably start using the "nice" library features of the language like STL and Boost and other total and utter crap, that may "help" you program, but causes:

- infinite amounts of pain when they don't work (and anybody who tells me that STL and especially Boost are stable and portable is just so full of BS that it's not even funny)

- inefficient abstracted programming models where two years down the road you notice that some abstraction wasn't very efficient, but now all your code depends on all the nice object models around it, and you cannot fix it without rewriting your app."

Yes... Yes. Yes!!! Finally. Somebody said it. Linus is now my hero!

Seriously, if you use C++ to its full extent - and look at inane amount of crap they put in C++0x ( - lambdas! concepts! alternative function syntax (yes, check THAT one out!), built-in iterators! - why not use C# or Java instead? Equally slow (*), at least they are well designed.

What used to be a very much WYSIWYG language with a very well defined purpose (speed and closeness to hardware) is now becoming a random subset of everything from JavaScript to Haskell, whether it makes sense to pack it in one place or not.

(*) STL semantics encourages runaway number of memory allocations which is very similar to C# and Java. The difference is that both Java and C# have memory systems that are designed for this purpose - the cost of object creation is very, very low - whereas in C++ it is anything but. (


Сергей said...

Man, you've got a really old post. It's like your grandma socks exposed :-)
Btw, Linus is not god.

NS said...

Ah, one of my favorites.

C++ is not a bad language. It just has a lot of lousy users.

Obviously, it's hard to disagree on STL - if you want to write Java, write Java.

C++ is C with classes, when used as such it is efficient and effective, and there's nothing wrong with it.

Anonymous said...

There is a comprehensive C++ critic by Yossi Kreinin with tons of examples why C++ is bad.

He also has a blog and in this post he writes that after years of C++ he starts to prefer plain C

Mikhail Poda

Anonymous said...

Beware of people who use only one bit to measure the quality of anything.

BadTux said...

Linus has been saying these sorts of things for over a decade now so I don't know about the "finally said it" bit, I remember him nattering on about idiocies in C++, hmm, must have been '96 or so when some durnfool suggested that it'd be really cool to write Linux in an object-oriented language like, err, C++. (I hesitate to name the durnfool who made that suggestion, err, ah. Yeah). Anyhow. My complaint is that Linus seems willing to throw the baby out with the bathwater.

Yeah, C++'s templates etc. are atrocities. One of my engineers proposed that the next version of one of our core apps be written in C++, and showed me some prototype code, and I pointed out that his overuse of templates basically resulted in an entire class being compiled multiple times with different types filled in for various parameters and therefore bloated up his code to roughly the same size as the Titanic. He had absolutely no idea of how templates were implemented by our C++ compiler or why overuse of the STL would result in code roughly the size and speed of a dead whale.

But that's not Linus's core beef. His core beef seems to be that object-oriented programming is evil by definition because you might get the object hierarchy wrong and therefore hard-code an inefficiency into the core architecture of the program that cannot be fixed without re-writing the entire program.

The problem is that you can do this with *any* architecture, whether object-oriented or not. "Object-oriented programming" is by its nature an abstraction on top of namespaces and structs with a list of functions associated with particular structs, rather than being something that is fundamentally different from non-object-oriented programming. Furthermore, those of us who've been doing object-oriented programming for many years a) have the experience to properly architect the object hierarchy in the first place, and b) have the experience to set up our objects so that they can be easily re-factored as we learn new things about the application set (i.e., we design for change rather than designing to meet only a specific fixed specification). For example, last week my CTO and VP of Engineering called me into a meeting and asked for a fairly major feature to be added to our new product and apologized for causing the need for a fairly major re-write at this late stage of the project. I said "No problem, I can think of three different ways that feature can fit into our object hierarchy with very little modification of the existing code, just new objects in the interstices." Because the architecture was designed with room for unknown concepts to be added in without major disruption of the basic architecture of the application. My engineers grumbled a bit over some of the things that I had them doing, but there was a reason for all of what I did, and now they're seeing it.

So anyhow, good architecture is good architecture, objects or no, and the same with bad architecture. Seems to me that Linus is confusing bad architecture with objects. Which maybe is something excusable if he's thinking about C++ objects, templates, and the STL/Boost, but he seemed to go beyond C++ and blame object-oriented programming as a whole as the cause of bad architecture. And I'm not sure that's defensible.