r/learnprogramming • u/TopicWestern9610 • Jul 01 '24
Linus Torvalds on C++
Post:
'When I first looked at Git source code two things struck me as odd:
- Pure C as opposed to C++. No idea why. Please don't talk about portability, it's BS.'
Linus Torvald's reply:
'YOU are full of bullshit.
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.
In other words: the choice of C is the only sane choice. I know Miles Bader jokingly said "to piss you off", but it's actually true. I've come to the conclusion that any programmer that would prefer the project to be in C++ over C is likely a programmer that I really would prefer to piss off, so that he doesn't come and screw up any project I'm involved with.
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.
In other words, the only way to do good, efficient, and system-level and portable C++ ends up to limit yourself to all the things that are basically available in C. And limiting your project to C means that people don't screw that up, and also means that you get a lot of programmers that do actually understand low-level issues and don't screw things up with any idiotic "object model" crap.
So I'm sorry, but for something like git, where efficiency was a primary objective, the "advantages" of C++ is just a huge mistake. The fact that we also piss off people who cannot see that is just a big additional advantage.
If you want a VCS that is written in C++, go play with Monotone. Really. They use a "real database". They use "nice object-oriented libraries". They use "nice C++ abstractions". And quite frankly, as a result of all these design decisions that sound so appealing to some CS people, the end result is a horrible and unmaintainable mess.
But I'm sure you'd like it more than git.'
Post:
'This is the "We've always used COBOLHHHH" argument.'
Linus Torvald's reply:
'In fact, in Linux we did try C++ once already, back in 1992.
It sucks. Trust me - writing kernel code in C++ is a BLOODY STUPID IDEA.
The fact is, C++ compilers are not trustworthy. They were even worse in 1992, but some fundamental facts haven't changed:
- the whole C++ exception handling thing is fundamentally broken. It's especially broken for kernels.
- any compiler or language that likes to hide things like memory allocations behind your back just isn't a good choice for a kernel.
- you can write object-oriented code (useful for filesystems etc) in C, without the crap that is C++.
In general, I'd say that anybody who designs his kernel modules for C++ is either (a) looking for problems (b) a C++ bigot that can't see what he is writing is really just C anyway (c) was given an assignment in CS class to do so.
Feel free to make up (d).'
The posts are quite old (2004-2007) adter reading the above, I just wonder what C and C++ (or anyone other) programmers and computer scientists have to say about the matter in 2024. Has much changed since then?
2
u/rygosix Aug 09 '24
I've been programming for 15 years now, and yes C++ is horrible.
In theory it was decent idea worthy of further effort in the 90's and into the 00's. In the 90's and 00's it added a handful of things that could have theoretically been nice over plain C.
Today, however? The only way C++ is good is if you basically ignore the majority of everything in the spec, in STL, and write it how you did in the 00's.
This is what any "good" C++ programmer I've seen does.
However, the issue with this is, the C++ committee abandoned them. Consequentially community follows the authority. Consequentially modern C++ is winning the majority. Unfortunately, this means writing "Good" C++ today will get you scorned by most C++ developers and fail out of most C++ interviews in any typical company.
All the new crap they are tacking on C++ makes it so slow that C# with latest .NET can now compete with C++ using all of its modern crap. Which means, if you really need the OOP, and all the enterprise architecture features, and all the encapsulation, and safety, just write C#. It will be comparably effective and way less of a nuisance.
However, if you still need performance, then go back to C.
C++ train got derailed.
377
u/freeky_zeeky0911 Jul 01 '24 edited Jul 02 '24
Strong opinions by highly intelligent computer scientists are a distraction, no different than any other field. Freud is considered the godfather of psychology, but all of his most "famous" students sought to prove him wrong. Einstein and Oppenheimer bumped heads but were friends and colleagues.Torvalds is not right about everything, but it sure sounds tastyš¤£š¤£š¤£. But what do I know, I'm just some guy.
207
u/OmnivorousPenguin Jul 01 '24
I'd say it's important to remember that the discussion is about kernel-level programming, not programming in general. The C++ abstractions indeed do not make much sense there, because the kernel operates at a lower level of abstraction, and so C is better for them. For application-level programming, you do want the C++ abstractions.
Right tool for the job and all that.
53
u/No_Window7054 Jul 01 '24
Oh thank fuck this was written in 2004-2007. I talked to a CS guy last week and according to him this would've been around the C++ dark age.
I'm trying to learn c++, and this almost scared me into thinking I'd made the wrong choice.
21
u/sessamekesh Jul 01 '24
I'm a VEHEMENT fan of C++ and use it in all my high performance projects, and I fully agree with Linus here. It's a bad tool for kernel development, and the people who pick only one tool and try to use it for all jobs shouldn't be allowed anywhere near such high impact code.
It comes with pros and cons like any other tool, for the Linux kernel and Git projects the pros are shared with C and the cons are painful and expensive.
For my little toy game engine the opposite is true - the pros over C are meaningful and the cons are shared with C. But even in that project, I frequently dip down to C for certain libraries and platform bindings that call for it.
Right tool for the job.
16
u/captain_obvious_here Jul 01 '24
Compilers have become much more efficient.
For the kernel programmers, this doesn't change anything to the fact that C++ is a bad language to use. But that's because a kernel is a very specific thing.
For everybody else, using C++ is a good choice, as long as you know what you're doing. By the way it was already that way in 2004-2007.
To sum things up, choosing the right language for your project is very important. And C++ is a language that can make things really bad if you use it without understanding what you're doing, and complete hell if you're a kernel writer.
27
u/abd53 Jul 01 '24
Not sure about kernel, never did it. But I do use dynamic arrays a lot. Am I going to use raw arrays with manual allocation-deallocation in C every time I need an array? No. Am I going to make something in C that handles the allocation and deallocation? That is what vector is. So, I'm just gonna use C++ happily. It's well portable, GCC (the compiler I like) is very reliable, it's performant, well structured. You can make crappy code in any language, some programmers writing crappy code is not a good enough reason to abandon a language altogether. On the other hand, it's not a crazy idea to select one language and stick with it for a particular project.
7
u/Whatever801 Jul 01 '24
Love reading Torvald's unhinged diatribes. Reminds me of this gem https://www.youtube.com/watch?v=iYWzMvlj2RQ. Part of it is he's an asshole. He named git after himself after all. Part is he's right I'm sure. There's no one on the planet more qualified to speak on this than Torvalds.
40
u/gywerd Jul 01 '24
Today, it is just a matter of temper, preference and need.
If you're into Systems Programming and drivers, ANSI C is you're platform-agnostic first choice. C is analogue to Assembly, meaning you can do anything - including targetting hardware directly and ruining the entire system.
For general purpose programming with OOP etc., type safe, modular C++ is your platform-agnostic first choice.
C & C++ might both be adequate for embedded programming - depending on constraints.
In Windows-land C# .NET has benefits - in Apple-land Objective C & Swift has benefits - in Google-land Go-lang has benefits - and in Android-land JAVA & Kotlin has benefits.
For web you have HTML5/CSS3, JavaScript, ASP.NET, PHP, Python, Ruby-on-Rails etc.
For X-plaform you might prefer JAVA, .NET MAUI etc..
Finally if you asked pioneers like Ken Thompson &Ā Dennis Ritchie, they'd tell you to make a new programming language for each different project. E.g. Ken Thompson developed the UNIX OS and B-language within a month, while Ritchie optimized B into the C-language.
6
u/[deleted] Jul 01 '24
I'll note his opinions on the matter were formed in 1992. It's compiler warnings were shit and linting tooling didn't exist then.
I promise you, C++-23 is a very very very different language.
And yes, some of the changes took on board the Torvalds criticisms where they were valid.
Exception handling. Yup. Still a problem, but there are plans afoot to fix that.
Why does it take so long?
To avoid Python 3 / Perl 6 style fck ups.
I will note that a lot of design effort in C++ language and library has been around zero cost abstractions, and they have been damn serious about that.
And when I look at the assembler generated these days.... usually I'd struggle to do better in C.
5
u/FriendlyBergTroll Jul 01 '24
As someone who learned C and had a small intro to C, whenever I look at C code, its very easy to understand unless its like super low level (but that is due to inexperience). The syntax makes sense and there is very little abstraction compared to other languages which really help me understand how things work
5
u/inarchetype Jul 01 '24 edited Jul 01 '24
...
...
I can't speak to the merits of his position here in 2024 because I have never done any system-level programming. But I'd suggest that it is important that his opinion be taken in context here- he is predominantly interested in systems programming, and the cost/benefit of abstractions, OOP, etc. is different. Sure Git isn't really sys tems programming, per se.. but the unique way Git works among VCSs comes from being concieved as a utility by a systems guy.
I have a brother who is an EE, who professes to hate C++, says that OO stuff is a bunch of nonsence and that it just reduces to stuff he can easily do better in C. But his programming experience is coding DSP chips for real-time processing, and writing driver test code (which he mostly does in Python). So I get than in his world, that is probably true. But it also doesn't generalize very well.
5
u/MarinoAndThePearls Jul 01 '24
It's funny seeing a genius hating a language you also hate because our reasons are very different and theirs are actually reasonable lmao. I just really dislike header files š
4
u/KC918273645 Jul 01 '24
I have written a small multi-threading embedded realtime OS using C++. I can say that C++ is a viable option for smaller systems, but if the project and the team size grow much larger, there are many things that can go wrong very easily. Using C for systems programming guarantees that "if something can go wrong, it will" will never happen at least because of the programming language itself.
7
u/StealthUnit0 Jul 01 '24
Older versions of C++ (pre-C++11, which is when those rants were made) lacked a lot of the features that make modern C++ good. C++ before C++11 and after that are pretty much completely different languages, and the rants that were made a long time ago don't really apply to the current C++ imo. This is even more true when you consider that older implementations of C++ (compilers/STL) had a reputation of being not as good as C implementations.
Whereas old C++ might have been pretty annoying to use, modern C++ is a very nice language that can be very productive and elegant for many tasks.
7
u/jucestain Jul 02 '24
Seeing as how Torvolds wrote LINUX AND GIT, I'm gonna take his word on it.
32
u/Divinate_ME Jul 01 '24
So the guy who made a name for himself by constructing systems based on meticulous logic opens his argument with "A tool is bad because it is mainly used by idiots". Why should I entertain a bad faith discussion like this?
8
u/deavidsedice Jul 01 '24
Most of it still is applicable today. Torvalds is specifically interested in low level code, for the kernel - keep that in mind. His thoughts could somewhat translate for regular apps, but the severity in which he talks would not make sense as a criticism for apps in C++.
I used C++ lots of years ago, and my main complaint on C++ is that it has overgrown so much that I can no longer say I really know C++, it is a beast of a language to know properly and manage up to today's standards. Can I produce workable C++ code? sure, but people with expertise will not be happy to see my rotten C++.
Also I got used to newer languages and toolchains (Python, Go, Rust) that make my life much easier and I do not want to go back to manually stitching libraries, Makefile/CMake/Scons/..
Should a beginner learn C or C++? In my opinion, generally no; unless they are targeting something specific where this languages shine; for example because they want to work with firmware, compute libraries, browser, video encoding algorithms, embedded systems, etc - and they want to get employed on that.
C and C++ can be replaced most of the times by Rust, which is way nicer. There are exceptions for this (plenty) and also getting a job in Rust, specially for a junior, is really hard today.
Python, Go and Java are usually better candidates as they can do most general applications that a beginner would be interested in, and they're a lot more modern than C or C++, which makes things way nicer and less frustrating.
Learning C can be also useful just for the sake of it, as it will give you a better understanding on how computers work.
C++ is not a bad language by any means. Just it is very ill suited for really low level stuff, and it is quite old and it shows when compared with modern languages.
3
u/Dziadzios Jul 01 '24
And then programs written in C keep using void*.Ā
3
u/RogueStargun Jul 02 '24
You know what's funny is I just learned what Monotone was earlier today. It was an open source VCS created by none other than a young(er) Graydon Hoare.
Maybe Linus' burn was so painful that Graydon went and created the Rust Programming Language out of spite. Work on Rust started in 2006 right after this sick burn.
And now Rust is used to write Linux kernel code.
5
u/Kaisha001 Jul 01 '24
I like Linus and I use C++. I'd rather people speak their mind and lay it all out then play games and never come to any conclusion. I think he'd be fun to have a beer with.
There are pros and cons to EVERYTHING in programming. The key is to understand the 'why', not just memorize the 'what'. WHY does Linus think this way is far more important than WHAT programming language you should use.
5
u/RolandMT32 Jul 01 '24
Linus Torvalds' opinions and comments seem unnecessarily harsh and extreme to me.. And several years ago, I heard that he apologized for years of being a jerk, and took time off to learn some empathy.
8
u/large_crimson_canine Jul 01 '24
Yeah heās abrasive or whatever but Iām gonna listen to the guy.
2
u/hugthemachines Jul 01 '24
I suspect that he may be right in that C++ is not better than C for the kernel but I doubt his notion that C++ devs are worse than C devs. Not because I have some statistics about that but just because it would be odd if only all devs using C++ are bad as a rule. It is hard (for me at least) to know if there is a separate reason for his experience, I mean some reason the C++ devs he encountered were bad in his opinion. It seems to me that the devs of any programming language are a mix of good and bad. I seriously doubt every C programmer is an excellent one. That would be strange, even if Linus indicates they are better.
2
u/ThyringerBratwurst Jul 02 '24 edited Jul 02 '24
My professor for computer engineering said in his lecture, when a portrait of Stroustrup was shown: "He looks like his language" (in reference to his kinda chaotic hairstyle).
I would personally avoid C++, if only because of the lack of native tooling: CMake is the best way to learn to hate your life.
And then there are all the quirks of C++, which are so subtle and constantly reproduce themselves every 3 years, making it impossible to ever fully master this language.
The Boost library is the best example of how this language became a corpse. There are actually no people left who still maintain this code or want to understand it. The industry is already having to make a lot of efforts to find people who will maintain their internal crappy C++ code.
I also think it's pretty pointless to program something in a language that will see itself as an "outdated style" in 10 years anyway, after 3 other standards have introduced new "correct ways".
2
u/lannistersstark Jul 02 '24
Angry toxic guy shakes fist at the sky, day 2948.
2
u/tk_kaido Sep 26 '24 edited Sep 26 '24
100% agreed. I'm no expert programmer. But when I picked up C++ and saw that the only main diff b/w struct and class is that struct members are public by default and class's are private. I was like damn, I'm getting out of here. Too many garbage and unnecessary "features", "abstractions", that require more mental bandwidth and hidden control flow. You can do the same thing a hundred different ways in c++ which is actually a really really bad thing. All this crap naturally leads to bad design choices and very difficult to maintain software. Not forgetting that it is a nightmare to write language compilers
6
u/BlurredSight Jul 01 '24
At the end of the day, Linus has the standing of possibly creating 2 of the longest running and most well maintained and most used codebases the world will probably ever see, and from that has made billions of dollars. He can rant about whatever he wants like every other billionaire it doesn't matter at the end of the day
4
u/antiproton Jul 01 '24
This post is not designed to reflect on the strengths and weaknesses of C and C++. It's designed to start yet another flame war between Torvalds critics and apologists.
It doesn't belong in a learn programming sub. This discussion is not pedagogical in any way.
3
u/hpxvzhjfgb Jul 01 '24
c++ is absolutely insanely complex, far more so than it was in 2007. as someone who started programming with c++, I would strongly recommend against using it for anything unless you really have no choice. for every possible thing that you might want to do, c++ has 50 different ways of doing it, and 45 of them are broken or considered bad practise. there are an enormous number of complex rules that you have to follow in order to write c++ correctly that the compiler simply will not tell you about or help you with at all, and which you would never even know existed unless someone told you about them. there's a saying that c++ is so big and complex that people only use 10% of the language, and everyone uses a different 10%.
if you want a good, highly productive, well-designed, modern c/c++-like language, do yourself a favour and just learn rust.
2
u/v0gue_ Jul 01 '24
I love Torvalds, not only for his contributions to modern computing and programming which include the linux kernel and git, but for his insight, talks, and opinions.
That being said, he often lives in his own expert blindspot bubble.
This is incredibly TRUE, but reality is that the realm of software development is massive and moves very fast. Until every programmer is as intelligent and good at the craft as Linus himself AND software must be meticulously crafted to perfection and taste, which is painfully obviously a silly pipedream to anyone besides a dev of Torvald's caliber, layman devs like you and me and basically 99% of developers need tools like C++ (and similar) to allow us to quickly and efficiently write production level code.
2
u/___s8n___ Jul 01 '24
javascript.
1
u/Historyofspaceflight Jul 01 '24
Idk I think things like templates and pass by reference are really nice. So I think there are useful features and abstractions that C++ offers
1
u/MysticNTN Jul 01 '24
š
1
u/MysticNTN Jul 01 '24
š
1
u/NormalSteakDinner Jul 01 '24
Me when he said it: š¤·
Me now: š¤·
1
u/dariusbiggs Jul 02 '24
Go look up C and C++ and UB (Undefined Behavior), it'll be eye opening, and you'll probably end up curious to see if this problem exists in other languages.
1
u/Timzhy0 Jul 02 '24
What, I think, people have a hard time come to terms with is the fact that, especially for system programming (even more for kernel dev in particular), you really do not want a programming language so opinionated and feature bloated as C++, because it's simply too risky. C being C, offers you much less opportunity to fuck up and allows you to create your own abstractions. So, even if this is much more work (some of it, fixed cost mostly like for data structures), it's still worth it. It's not truly reinventing the wheel because for e.g. your approach to memory allocations and errors may be completely different that the "builtin" one of C++ which pretty much you cannot get rid of, once you use literally anything in the std. I'll especially emphasize Linus' claim about languages that hide allocations (and which often also have a GC), I hope it became apparent now that none of them would be a fit for system programming, again, way too opinionated. The reason these language often go that route is to be able to provide ergonomic and, to some extent, safe abstractions which IMO are much better suited for general purpose app development. For ergonomics sake, they must assume something about memory management (e.g. otherwise imagine every time you concatenate a string, you need to pass along an allocator, that's not exactly ergonomic right?), and for the latter they must decide how to represent and handle errors (e.g. exception based).
1
u/cantthinkofaname1029 Jul 02 '24
I work in HW/embedded, and 90% of the comments I see about the cons of c++ vs C are straight up misinformation, lies, or just plain old not knowing how c++ even works. It's great for performant code and all types of low level work...
...if you know which parts to use, which is the big con of C++. It's vastly more powerful than C, and thus requires much more discipline to use
1
u/Miserable-Alarm8577 Jul 04 '24
LOL!!, That's a classic Linus response. Always been highly opinionated. But he earned my respect for 40 years. I hear he likes rust though
1
u/[deleted] Jul 04 '24
How can I get to the level to either agree or disagree with this? I don't understand what's going on lol
1
u/mrdannik Jul 05 '24
Agree with Linus. C++ is a shit language. A backwards-compatible x86 assembly shit-show of higher level languages that has no place in the world where C/Rust/Go are options.
1
u/Afraid-Teacher-6105 Oct 03 '24
Well. I agree yet if you started thinking about not everything is perfect. Your harddisk is buggy, your RAM is buggy, 01 bits can flip at any direction at any point in time. You won't care that much about "absolute accuracy." When you think every program is just an executable, including the kernel - is an executable from another kernel. When you understand programming, that it's chances, a sequential of SUCCESS operations, then maybe you look at programming a different way. Learn to compromise every program, try to get a grasp of defined and undefined use cases - double check the output - use it. Don't think your program is the last of mohicans. It's really not. People don't trust you, even if you are God
1
u/cimmic Jul 01 '24
It's hard to take his arguments seriously when he offers them like that. I'd never want to work for someone that talks like that.
0
u/[deleted] Jul 01 '24 edited Jul 01 '24
oop is just a bunch of crap, that I'm pretty certain of
the peculiarities of the low level issues of C++ I'm not that familiar with but my gut feeling is to agree with the genius here...
I've been working for nearly 20 years and in no case I've met a person I really respected defend oop beyond the.... we're doing high level straight forward things in a very popular library with an ecosystem that's been tried and tested thousands of times
there is a market for that however and it's much more likely you'll end up coding that high level thing on a popular library (and paid well for it) than working on the linux kernel
we all have pretty strong opinions, and I feel that's because we've been damaged by our experiences, but I find the more you stay away from any oop the closer you get to the smarter people, there is just no positive aspect to it beyond encapsulation, which is not unique or novel to oop
1
u/adithati Jul 01 '24 edited Jul 02 '24
If he has such strong opinions about C++ which is still closer to hardware. Not sure what he would say about Java .
1
u/[deleted] Jul 01 '24
First off, I respect Linus a lot for his work, but these takes are completely outdated.
Smart pointers were introduced in what, C++11? Look at all the progress since then. The language is much safer overall. It has better templates. We have concepts for metaprogramming.
Like sure, go ahead and use C for your kernel, but get it right.
Don't complain when a RustOS has better reliability in 10 years because oh wait... writing 1,000,000 lines of accurate C code is really hard, and everyone makes mistakes sometimes.
0
u/ejpusa Jul 02 '24 edited Jul 02 '24
Itās not a fun language. In the AI world? Itās all Python.
Python is fun! C++? You outsource that to India. Itās just not sexy. So outsourced it goes.
:-)
-3
u/buldozr Jul 01 '24
Not really. C++ is a really unfortunate, haphazardly designed language.
-1
u/IchLiebeKleber Jul 01 '24
Interesting that (at least at one point) Linus still preferred KDE over GNOME even though the former is entirely written in C++.
0
u/[deleted] Jul 01 '24
Hw is correct of course, except in the way he chose to communicate his position. When you're speaking about design choices of important software like git and Linux, more professionalism is expected. Unfortunately, most people in this space do not value professionalism, respectful tone and clear communication enough. It shows.
0
u/_alter-ego_ Jul 07 '24
What has changed is that Python [and most recently, chatGPT] has arrived... Similar characteristics & consequences, but 1000x amplified/worse. I don't know how we will survive tomorrow. When I see what "programmers" are doing today ... they do not think any more (about code and algorithms -- of course they do think that they are good at what they do, they even write blog posts and tutorials š).