Linus Torvalds and C++

Started by
149 comments, last by Anon Mike 16 years, 4 months ago
Hi, I've got some code on the internet, which are C++ libraries, and sometimes it happens that I get an email from someone who asks if it's possible to have a C version. I've tried to convert one of my libraries to C once, but I gave up when I realized how bad the code started looking: not that C is bad, but it feels really bad to have nice C++ code that is very readable and uses classes and the STL, and then convert it to C where everything is much more limited, and you're basically converting all your classes to structs and convert member functions into functions that take the struct as first parameter, and don't have std::strings at all and have written some sort of replacement for std::vector that is also a struct with std::vector-like functions that uses malloc and realloc and such everywhere, but has to use void*'s to avoid code duplication since there are no templates in C. And did I mention how ugly the comments start looking if you can't even use "//" style comments? Or the thing where you have to declare all variables first, only then write the code? This all helps making code that was nice, into something less nice, which caused me to give up the conversion to C effort and keep my libraries C++. Anyway, I wonder, why are there people who need C? Maybe it is because Linus Torvalds has something against C++? I looked that up in Google, and found this: http://emonk.debianuruguay.org/?p=42 It's from september 2007, so very recent! In it is a lot of C++-bashing, including things like this:
Quote: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.
As well as this:
Quote:- 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)
I use STL everywhere, how's it not portable? My code compiles on all major OS platforms. C++ makes everything much nicer IMHO while C has a lot of annoying limits. Why is C++ so hated in a lot of the open source world?
Advertisement
Part ignorance, part incompetence (hey, he says it himself, there's an awful lot of C++ programmers around. I'd say it's a fair bet he's one of them. And yes, that's a very valid reason not to use C++), and finally, part of it is that C is just more convenient for kernel development, which is obviously a major focus for Linus (and holds a kind of symbolic value to a lot of Linux/open source projects. If it's suitable for kernel development, it's suitable for Linux, and then it's suitable for open source in general.
Maybe these people asking you for C versions of your libraries want to be able to use them with other languages, like Python or Perl?

And you don't need to rewrite your libraries in C, just provide an external C interface/wrapper if possible.
Quote:
C is just more convenient for kernel development

I personally find C++ more convenient for kernel development over C, tbh. (I personally use MSVC++ 2005 for my kernel) This is all opinion, of course.
His first quote seems well-justified to me (the second one, less so). By most measures, C++ is a horrible language that can be coaxed into doing beautiful things. It is akin to Frankenstein's monster: a patchwork of features sewn together with very little cohesion and many lousy interactions. Besides, it's difficult to use it because of its many inherent pitfalls, which leads to the existence of many extremely bad programmers.

But that is not a serious reason. There are two serious reasons why C is used instead of C++ for Unix systems programming.

The first reason is historical: the Unix dynasty of operating systems was mostly developed in C, and so systems programming today is done in an environment where most legacy code is C code, where most system applications are small standalone bits of code which interact with existing C bits of code, and where most developers are fluent in C and perhaps one or two shell scripting languages (SH, sed, awk, PERL...). In such an environment, attempting to develop a systems-level tool in C++ will cause problems: interaction with C legacy code reduces the scope of any benefits derived from using C++, and interaction with a C developer base reduces the user base of your program.

The second reason is practical: while C++ is a fairly decent application development language, it's a very bad systems programming language. C++ is too complex and too rigid to support the number one activity of many open source system administrators: hacking (in the MIT sense).
Let's put it another way: I consider myself to be an expert C++ programmer. The thing is, I need to be an expert C++ programmer in order to write relatively bug-free code. I've been using the language for something like 15 years, and I still have WTF moments when my code behaves in a manner completely different than I intended. I'm probably only a decent Python, Visual Basic and C# programmer, but I don't need to be any better than that to get my programs to do what I want in those languages.

Quote:
And did I mention how ugly the comments start looking if you can't even use "//" style comments? Or the thing where you have to declare all variables first, only then write the code?

Upgrade C compilers. The C99 standard has allowed // comments and intermixed declarations and statements for about 8 years now.

Of course, STL and boost are portable libraries. For a given value of "portable" anyways. Many of the boost libraries don't come within spitting distance of running on anything other than Windows or POSIX machines, such as Classic Mac, many embedded systems or many game consoles. And if you're in an environment where you can't or won't use exception handling, which is very common in low level systems programming or on platforms with truly craptacular exception implementations, then you really shouldn't consider using most of the C++ standard library, since much of it uses exceptions for indicating error conditions.
Quote:Original post by Lode
http://emonk.debianuruguay.org/?p=42

It's from september 2007, so very recent!

This only reenforces an opinion I got from reading quite a few of Thorvalds posts on kernel dev mailing lists: he is actually not the programming god many enthusiasts would make him to be. He is a highly unprofessional, arrogant, incompetent asshole. There, I said it.

Of course, there are many things that can be said about C++, both good and bad (probably more bad than good though ;). Arguments from both sides have there merits. However, Thorvalds has absolutely no idea what he is talking about. If you don't know shit about the language you are ranting about, and your only motivation against it is your zealotry, then you'd better shut up. The fact that he presents his arguments with the eloquence of a five year old child doesn't really help his credibility either.

God, this guy really goes on my nerves.

Quote:
Part ignorance, part incompetence

Add a good dose of zealotry induced cognitive dissonance to that.

I'd really like to hear Thorvalds opinion on C#. Not for kernel development obviously, just as a general purpose language.
Quote:Original post by Crypter
Quote:
C is just more convenient for kernel development

I personally find C++ more convenient for kernel development over C, tbh. (I personally use MSVC++ 2005 for my kernel) This is all opinion, of course.


C# is much ore convenient for kernel development.. linus torvalds is really old-minded.. :)

oh, and btw, if you think, it's a joke.. google for singularity.. i really like c# kernels :)
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

outRider is right, it has to do with the interface, because of different compiler's ABIs you compile a C++ library with MSVC and you wont be able to link it against anything in any other compiler (except perhaps different versions of MSVC).

The solution is to write all internal code in C++ but provide a C interface to the outside world, if you already have a C++ library, write a wrapper for it, you don't really need to write everything in C.
Quote:Original post by Yann LHe is a highly unprofessional, arrogant, incompetent asshole.
That is almost exactly what I was about to post, before seeing it had already been said. I'll debate the "incompetent" charge; I'm sure the guy can code. But he embodies everything which makes open-source zealots exactly the kind of people you wouldn't really want as colleagues.

This topic is closed to new replies.

Advertisement