Skip to main content
GameDev.net gamedev.net
🔒 Locked

Linus Torvalds and C++

Started by Lode Dec 28, 2007 at 6:20 AM 149 replies 39.3k views
Original Post
Lode
Lode
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?
Spoonbender
Spoonbender
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.
outRider
outRider
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.
Crypter
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.
ToohrVyk
ToohrVyk
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).
SiCrane
SiCrane
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.
Yann L
Yann L
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.
davepermen
davepermen
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
Kwizatz
Kwizatz
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.
d000hg
d000hg
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.

shotgunnutter
shotgunnutter
Linus is a typical social inept. High logical intelligence, low social intelligence. And he is surrounded by open source zealots who refuse to tell him so. So it turns out he hates c++. Despite having the support and authority to improve the langjuage, he goes on a rant and knocks it. He also has a "them vs us" attitude towards certain OSes and certain Languages.

I have some code for Mr Torvolds to read, should he frequent these boards (I suspect aersixb9 is in fact Mr Torvolds)

Task task;Tool tool;Person person;if(person.canUseTool()){	if(bestToolForTask(tool,task)){		useToolForTask(tool,task);	};}else if(person.isConceited()){	person.decideToolIsCrap(f);	if(person.isNarrowMinded() {		person.dissThoseWhoUseTool(tool);	}		if(person.isPublicFigure()){		person.preachOpinionOnToolAsFact(tool);	}}else if (toolIsPopular(t){	person.learnMoreAboutTool(tool);}else{	person.findMoreSuitableToolForTask(task);};
I just wanted to see if he would actually do it. Also, this test will rule out any problems with system services.
taby
taby
Hey, that's complete and utter crap. In fact, I port my STL code to his hacked together non-standardized OS from hell without any problems. Are these quotes actually real, or does he actually have his head up "in the clouds" that far?
j_smith4
j_smith4
Guess I have somebody to lookup to since I'm a C and procedural progamming type of guy.
--------Ratings - Serious internet buisness
OrangyTang
OrangyTang
Quote:
Original post by Lode
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.

"Portable" means many different things to different people depending on context. If I use "portable" then I'm almost certainly refering to something that works across linux, win32 and mac. For other people it might mean ps3, 360 and win32. For OS people it's more likely to include linux, win32, mac, solaris and a whole bunch of obscure platforms that practically no one ever uses yet are still important to them for various reasons.

I remember watching his presentation about GIT (IIRC it's on YouTube somewhere) about why it's so much better than every other source control system. He's obviously a smart cookie, but his presentation style and communication leaves a lot to be desired. He'd often make sweeping statements which may very well be accurate from his POV (like, CVS is terrible and SVN isn't any better) but without supporting arguments (like "CVS is shit for kernal development because it's not distributed" which is what I think he meant) he just sounds arrogant and not worth listening to.
Spoonbender
Spoonbender
Quote:
Original post by davepermen
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 :)


I know about Singularity. But I doubt you've written a C# kernel yourself [grin]
But my point (and perhaps I should have been clearer) is that it takes a lot more infrastructure to get C++ (or C#) up and running in your kernel. C is relatively straightforward among other reasons because it's smaller, has a tiny standard library, and has a well-defined ABI. (and you will need to call into it from the bits of ASM you have)

So at least for parts of the kernel, C is an obvious choice of language. Of course it doesn't need to spread to the entire OS, but that's what happened with the *NIX family.

Quote:
Quote:
He 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.


Incompetent C++ programmer. I don't know enough about his coding skills to judge him as a kernel (or C) programmer, but I'm sure he's good enough to get the job done there.


As SiCrane said, the thing about C++ is that you need to be an expert to avoid shooting yourself in the foot. That's a fair criticism of the language. Linus isn't an expert C++ programmer, so he shoots himself in the foot, and so he doesn't like it.

[Edited by - Spoonbender on December 28, 2007 10:42:54 AM]
Promit
Promit
Quote:
Original post by Yann L
He is a highly unprofessional, arrogant, incompetent asshole.
I'd like to amend this:

He is a highly unprofessional, arrogant, incompetent asshole. Furthermore, you'd have to be a completely blind fool not to realize this almost immediately.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
ToohrVyk
ToohrVyk
Quote:
Original post by Spoonbender
Quote:
He is a highly unprofessional, arrogant, incompetent asshole.

Incompetent C++ programmer.


You mean, he's competent at being an asshole?

Just askin'
capn_midnight
capn_midnight
Quote:
Original post by Promit
Quote:
Original post by Yann L
He is a highly unprofessional, arrogant, incompetent asshole.
I'd like to amend this:

He is a highly unprofessional, arrogant, incompetent asshole. Furthermore, you'd have to be a completely blind fool not to realize this almost immediately.

+1. I find it hillarious that he uses what other people do in C++ as a reason to not use C++. If that were a valid reason for not using a language, I wouldn't have learned Java, C#, or VB.NET. I like to think that my competency in these languages is why I'm paid more than the incompetent people in the field.
rip-off
rip-off
Of course, he is completely right.

Except about choosing C instead, but that stems from the fact that he is primarily a kernel developer.

To be perfectly honest, C++ is a horrible language. For low level code, it is too unwieldy. Once you cut the runtime, you cut exceptions, which cuts a lot of the STL, which leaves the language looking pretty unimpressive.

For high level code, it simply isn't high level enough.

I prefer writing C++ to C, because I like RAII and the Standard C++ Library and boost. But I prefer writing other languages to C++.

The only place it is useful is in those areas where we'd like to write in a real high level language but cannot because said languages cannot provide the performance we need. Thankfully I don't feel the urge to make fancy games that stress modern computers, and can get by with having lots of code in a scripting language and a slowly reducing amount in C++.
d000hg
d000hg
Surely even without exceptions and STL, classes and encapsulation are a benefit? Sure it's harder to exactly what will happen when, but a good C++ programmer will be able to.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.