Slashdot Mirror


Was Linus Torvalds Right About C++ Being So Wrong?

Nerval's Lobster writes: Perhaps the most famous rant against C++ came from none other than Linus Torvalds in 2007. "C++ is a horrible language," he wrote, for starters. "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." He's not alone: A lot of developers dislike how much C++ can do "behind the scenes" with STL and Boost, leading to potential instability and inefficiency. And yet there's still demand for C++ out there. Over at Dice, Jeff Cogswell argues that C++ doesn't deserve the hatred. "I've witnessed a lot of 'over-engineering' in my life, wherein people would write reusable classes with several layers of inheritance, even though the reusable class wasn't actually used more than once," he wrote. "But I would argue that's the exception, not the norm; when done right, generic programming and other high-level aspects of C++ can provide enormous benefits." Was Linus going overboard?

29 of 757 comments (clear)

  1. Write-only code. by mellon · · Score: 5, Insightful

    The problem with C++ is that it's way too easy to write write-only code, because the language has so many features that nobody but language experts understand all of them. So we all program in different dialects, and then scratch our heads when we read other peoples' code.

    1. Re:Write-only code. by Anonymous Coward · · Score: 4, Insightful

      This is a potential problem with every single programming language ever. No exceptions. C is somewhere in the middle with that risk, but it is no where near the worst. I'd give that medal to Perl.

    2. Re:Write-only code. by Carewolf · · Score: 5, Insightful

      The problem with C++ is that it's way too easy to write write-only code, because the language has so many features that nobody but language experts understand all of them. So we all program in different dialects, and then scratch our heads when we read other peoples' code.

      Less so than C, especially as used in the kernel. Seriously read some of the Linux kernel and compare it with any good C++ project. The kernel loses BADLY. The manually implemented virtual classes are not pretty and not type safe, and neither are all the ugly macros needed to do things that would safe, automatic and easy to read in C++.

    3. Re:Write-only code. by loonycyborg · · Score: 4, Insightful

      I definitely disagree here. All features of C++ have their uses and it's possible and necessary to master them all. This language is focused on getting the job done. Thus some of its design decisions don't look very nice. But it's the most usable language in its niche.

    4. Re:Write-only code. by KermodeBear · · Score: 5, Insightful

      The same applies with Perl. And PHP. And Java. And Go. And Ruby. And Python. And Javascript.

      I've also seen good C/C++, PHP, Java, Python, etc.

      You can write crappy convoluted code that uses odd, unusual frameworks and features in every language. It isn't the fault of the language - it is the fault of the people (ab)using it.

      --
      Love sees no species.
    5. Re:Write-only code. by sjames · · Score: 3, Insightful

      Actually, the kernel code is quite clear and understandable to any decent C programmer. I find it hard to consider it losing BADLY to anything.

      Note that C++ isn't necessarily type safe either. It's not at all difficult to cast an object pointer to the wrong type and crash. It's no harder than it is in C.

    6. Re:Write-only code. by Penguinisto · · Score: 2, Insightful

      This is a potential problem with every single programming language ever. No exceptions. C is somewhere in the middle with that risk, but it is no where near the worst. I'd give that medal to Perl.

      The more powerful the language, the more it's like a loaded gun: You can use it responsibly and do amazing things with it, or you can put a bullet through your foot with it. Choice is yours... and the closer you get to bare metal with the language, the greater the chance of lead meeting foot at high speed.

      --
      Quo usque tandem abutere, Nimbus, patientia nostra?
    7. Re:Write-only code. by wed128 · · Score: 4, Insightful

      Sometimes wounds itch. When you scratch them, they break open and fester. C++ scratches exactly these itches.

    8. Re:Write-only code. by Rei · · Score: 4, Insightful

      The sample code will copy a and b twice, once to put them in the lambda closure

      Only without optimization flags enabled. Otherwise the lambda will be inlined in most cases.

      This is the general problem with C++, in that the shortest code is often the slowest

      Compared to what alternative C algorithm? Still waiting here.... ;)

      Beyond that, that's not how you optimize a program. You don't try to optimize the heck out of every last line; that's a recipe for an unmaintainable mess that's not actually that fast. You write clean, organized code, you profile it, and then you optimize where the profiling data tells you that you can make the biggest improvement. Trying to be "clever" and outsmart the compiler with every line of code "in the interest of performance" is how spaghetti code gets made

      The slowness is visible in C, where you would probably

      To reiterate my first post:

      Show me the code (emphasis: show me actual code, don't just say "... this is how I'd do it" and a rough description

      Yes, I knew exactly that people like you would come in and try to pass off a rough description as an implementation. The reality is that the implementation is such an utter, bug-prone PITA in C (compared to a trivial command in C++) that most people don't even bother. Which means that code gets underthreaded, which means that it performs terrible and is prone to lockups.

      Threading is of course just the start here...

      --
      "Are you hungry? I haven't eaten since later this afternoon." -- Primer
    9. Re:Write-only code. by TheRaven64 · · Score: 3, Insightful

      This is true. On Linux, the only compilers that I know of that use the same C++ ABI as gcc are clang, ICC, XLC, ARMcc, Open64 and Path64. If you're using something else then you may encounter problems.

      --
      I am TheRaven on Soylent News
    10. Re:Write-only code. by serviscope_minor · · Score: 5, Insightful

      because the C++ standards committee got a bee in its bonnet about the latest hot new concept that first came out in 1959 and was forgotten until last year

      Give one example of that ever happening. The C++ standards committee is notoriously conservative which is why the language evolves so slowly. This comes across as you just making shit up when you haven't got a clue.

      The problem with possessing multiple ways to solve a problem is that every developer takes it as a personal challenge to find and use all the different ways.

      That might be *your* problem but it isn't mine. Every decent place (staffed by true scotsmen of course) with project teams has things like coding standards and code reviews.

      If your developers are committing crap code to the mainline then the trouble is that (a) your developers and (b) your process sucks. The thing is it will suck in any language.

      The library is here: Extended C library, libxc, so help yourself (BSD license)

      OK, I'll bite.

      Well, if you think having XVEC_DEREF(some_vector, i, float) for resisable arrays but array[i] for builtin ones is better than having array[i] for both, well, then I guess we're just on completely different pages here.

      The thing is there's massive syntactic overhead to your code. And the overhead is more like 1000% not 150%.

      vector<float> = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9};

      works perfectly in C++, whereas that's 10 lines of C code!

      And I was wondering how it worked underneath, and I had to dig into the code. So you're storing pointers to the elements, not the elements themselves! I never even realised that. Wow, so that means you can't even do:

      xvector_ins_tail(tfar, &1f);

      You have to do:

      some_float_which_has_the_same_scope_as_tfar_and_is_never_reused = 1;
      if(!xvector_ins_tail(tfar, &some_float...))
      { //Do something error related here //probably a longjmp or goto. //Don't forget to free all that memory!
      }

      versus:

      tfar.push_back(1.5);

      It also means you're in nightmare memory management from hell since you now have to manage all those bits of memory by hand.

      It *also* means that you have no memory locality since it's a big array of pointers, so performance will almost certainly be bad as well.

      The equivalent C++ code to your xvector/main.c is something like:

      vector<float> sfar = {1.1, 2.2, ...};
       
      for(const auto& f:sfar)
        cout << f << endl;
       
      eg_t eg[] = {...};
       
      vector<eg_t> teg, sub;
      for(const auto& e:eg)
        eg_t.push_back(e);
       
      for(auto e:teg)
      {
        e.i += 2;
        e.f -= 5.0;
        sub.push_bach(e);
        cout << "Returning " << e.i << " // " << e.f << endl;
      }
       
      //Well, the nativea bit is basically already working in the C++ code since internally, std::vector uses a native array

      And that gets us to line 90 or so. There's more, but you missed the error checking for if something isn't found in the xvector. It also took me a while to verify that all the allocations are matched by the respective free. It would be nearly impossible to be sure for less trivial code.

      So, I'm not convinced you've made a case for C over C++ here.

      --
      SJW n. One who posts facts.
  2. Almost got me by halivar · · Score: 5, Insightful

    I always get halfway through a Nerval's Lobster summary before my anger/indignation/smug validation gives way to the sad realization that Dice has trolled me yet again.

  3. Re:Ugh by mellon · · Score: 4, Insightful

    Oh no, on the contrary. There are plenty of idiots who can write code in C++.

  4. C++ by Anonymous Coward · · Score: 1, Insightful

    C++ deserves all the hate it gets. Life is too short and mental health too precious to become an expert in C++.

    1. Re:C++ by Anonymous Coward · · Score: 4, Insightful

      Life is tough, but it's tougher if you're stupid.

  5. No by Anonymous Coward · · Score: 2, Insightful

    Linus was not going overboard. In this rare instance, I'd argue he hasn't gone far enough.

    1. Re:No by boristhespider · · Score: 5, Insightful

      A clickbait article about a flamebait rant, commented on by trolls.

      God bless Slashdot.

  6. Ahhhh, C++ by segedunum · · Score: 2, Insightful

    Although the language itself isn't truly, truly bad, the only thing that made it tolerable was a library like Qt. STL and Boost are so shocking it isn't even funny, as well as the attitude amongst many that they are some kind of 'standard'. That's where C++ really did go off the rails.

    1. Re:Ahhhh, C++ by gnupun · · Score: 4, Insightful

      Although the language itself isn't truly, truly bad,

      Any language that allows the programmer to override the '=' operator is truly, verily, bad. That makes the language over-programmable and a write-only language when trying to maintain other developer's code. Java may have fewer features, but that doesn't stop the developers from getting stuff done.

  7. Python/C++ Combo by EmperorOfCanada · · Score: 4, Insightful

    I have read maybe 1 billion articles about language X being better than language Y and in many cases it is pure religious fanaticism; someone has committed to a language and now justifies that commitment with zealotry. A very common refrain about any given language is how many people write poor code in that language. This argument is often reserved to support the more "sophisticated" languages. For instance it is pretty much a gold medal sport to crap on PHP; and yes there is lots of terrible PHP which probably stems from the fact that it is often someone's first language and that someone is self teaching.

    Then other languages are looked at as toy languages by those who resent them, Python would often be a victim here.

    Then there are the wonderful charts of speed which in theory would justify everyone using ASM optimized to their CPU.

    But for me it is not one language but a pairing that has caught my heart. Python and C++ do just about everything I want. Python is just so damn productive. Then I use C++ for where Python falls down on speed or the environment itself is not conducive to C++ (embedded and multi-platform Mobile).

    But to answer his cry about people over-engineering things with silly STL uber inheritance type crap. That is where oddly enough the zealots of C++ are their own worst enemies. They love C++ so much they are giving it a bad name. Many people use STL in some purist way that completely blows Keep It Simple Stupid out of the water.

    But I really do hold a special revulsion for anyone who claims that their language "Enterprise" which translates to me as so shitty that nobody will notice that most of your drone developers are also shitty.

  8. Yes, he was wrong... by RailGunner · · Score: 5, Insightful

    If you care about performance, the ability of C++ to "run on the iron" is a valuable tool to have in your arsenal. Add in inline assembler, and IF YOU KNOW WHAT YOU'RE DOING you can write blazing fast code in C++ and still provide a sensible code architecture.

    There's no sense in blaming the language for the abuses developers have written -- you might as well indict English for the horrible spelling and grammar of many Americans...

    If you know what you're doing, C++ is a terrific, powerful language suitable for a plethora of projects. On the other hand, if you don't know what you're doing, well, I guess there's Visual Basic or C#.

  9. True across the board. by Frobnicator · · Score: 3, Insightful

    Linus is doing systems level work. At systems level work, there are a lot of mediocre and bad programmers who use the common language of C++. Those who know c well are unlikely to be the mediocre and bad programmers.

    That is really a truism across all fields and languages.

    In the business world with business logic, there are a lot of mediocre and bad programmers who use the common language of Java. You can filter out many of them by adding a skill requirement of some other less-used languages inside that realm of business software development.

    In a field where everyone is doing Ruby development and you don't want mediocre/bad Ruby programmers? Require them to also demonstrate proficiency in another language.

    In a field where everyone is using C#? Require them to also demonstrate proficiency in C++ or some other language.

    If you only require a single thing you can get unskilled individuals with only a single skill. If you require multiple skills you are more likely to get more talented individuals, since the talented, higher producers tend to pick up a wide range of skills.

    --
    //TODO: Think of witty sig statement
  10. Re:depends upon what you're making by sjames · · Score: 1, Insightful

    I wouldn't use it for most firmware either. Firmware often has size constraints that C++ is less than friendly about. It also often runs in a limited environment that may not be friendly to C++.

  11. Aren't all (but one) popular languages like this? by Anonymous+Brave+Guy · · Score: 4, Insightful

    This is all true, but I'm not sure how it's any different to almost any other popular language.

    Java and C# have also evolved a lot of new language features in recent years. For many types of software, the way the code looks will also be heavily influenced by which libraries and frameworks are used in that project's stack.

    It's the same story for web development. We have different flavours of JavaScript (ES5 in most browsers today, but ES6 just around the corner and supporting a wider range of programming styles), Python (2 vs 3), and so on. And with these more dynamic languages, the style is often even more guided by a framework if you're using one.

    Even if you're not using pervasive third party frameworks or libraries, any project of non-trivial size is going to adopt its own conventions and build its own abstractions to suit its particular needs, and then the rest of its code will again become its own dialect written in terms of those conventions and abstractions.

    In fact, I can't think of any mainstream language except for C that doesn't suffer from the "dialect" problem to some extent. And that's because C is a 20th century language in a 21st century world, so lacking in expressive power that it can't support any of these modern, high-productivity development styles and abstraction tools. Its ubiquity, portability and simplicity are assets, but they are effectively its only redeeming features in 2015, and as time goes by it will be necessary for fewer and fewer projects to choose C for those reasons.

    "There are only two kinds of languages: the ones people complain about and the ones nobody uses." -- Bjarne Stroustrup

    "If you attack a tool based primarily on not liking the people who use it, you're still just a bigot, no matter how famous you are." -- Anonymous Slashdot poster

    --
    If you disagree, post your argument. (-1, Overrated) isn't your personal censorship tool for views you don't like.
  12. Re:Aren't all (but one) popular languages like thi by Wootery · · Score: 3, Insightful

    I can't think of any mainstream language except for C that doesn't suffer from the "dialect" problem to some extent

    What about GObject?

    As you say, C is a very bare-bones language, so it's not uncommon to see the object-oriented wheel reinvented as a C library, incompatible with the other such reinventions.

  13. Casting by ziggystarsky · · Score: 3, Insightful

    Casting is telling the compiler to do what you want. It's like saying "Shut up! I know what I'm doing, this thing is a XY pointer, even if you can't figure it out yourself."

    In every language (which supports casting) you can make an error while casting by claiming something that isn't correct. Surprise!

    1. Re:Casting by Dutch+Gun · · Score: 5, Insightful

      Modern, properly designed C++ code is absolutely safer than C code. However, because C++ is a superset of C, you can obviously write code that's just as unsafe as C, simply by ignoring the best practices and writing "C with classes" (which many do). A lot of what you can do in C++ exists solely to provide backward compatibility, both with earlier versions of itself as well as with C.

      C++ gives you the ability to create new types using objects, which you can operate on both through member functions as well as logical operator overloading (where it makes sense to do so). For instance, you could create a class for handling file paths (as opposed to using raw character pointers or arrays, or even C++ strings), and when that class is properly developed and debugged, you can then be confident that you no longer have to worry about accidentally creating a security vulnerability or introducing a crashing bug. Moreover, it can handle path-specific things, such as ensuring proper form when paths are concatenated. Even better, when compiled down, it's really no different than code written in C, since C++ still adheres to the "zero-overhead" principle for most features.

      When people talk about C++'s "dangerous casts", they're almost universally taking about "C-style casts", which are discouraged in modern C++. Instead, you should use the more explicit casts, which either use static compile-time checking or even run-time checking as appropriate. Whenever you have to resort to a C-style cast in C++, you had better have a *very* good reasons (in many cases it's just a design failure). Nowadays, that also includes managing raw memory or raw pointers thanks to the addition of standardized smart pointers.

      This is why C++ is almost universally used in the videogame industry (I work as a videogame programmer), because is strikes a reasonable balance between safety, advanced language features, and performance. It's also nicely compatible with C libraries, with which we often have to interface at the OS level, or when using 3rd party libraries. And finally, while "better" alternatives arguably do exist, C++ is also well supported and extremely ubiquitous across the industry. As the saying goes, "quantity can have a quality all it's own". This is important when trying to hire experienced developers, or looking on the web for solutions to a problem.

      --
      Irony: Agile development has too much intertia to be abandoned now.
  14. Horrible compared to what? by hey! · · Score: 4, Insightful

    Compared to C, sure. C was conceived in a spirit of pragmatic minimalism that's easy to love. I remember learning C from the K&R book back around 1980. That book was so thin it was practically a pamphlet next to books that taught you other languages. Everything about C was so neat, and trim, and cogent -- even the book everyone learned it from. That plus The Unix Programming Environment and perhaps Software Tools and you were cooking with gas.

    It's natural to compare C++ to C; the very name encourages you to do so. It was to conceived to dovetail and build upon C. But it was conceived with an almost diametrically opposite kind of philosophy. C chucked out all the precious features that designers were putting into languages in the late 60s and early 70s and went with a tiny set of proven useful features. C++ implemented every bell and whistle anyone had ever dreamed up for object oriented programming, which was largely an academic topic that was full of clever but impractical notions. Well, it turned out that a lot of those things like operator overloading and multiple inheritance weren't all that useful in the judgment of later language designers, but you can hardly blame Bjarne Stroustrup from knowing that in advance.

    It's practically impossible to overstate the practical success of C++. It took what was for most practicioners a theoretical idea (object oriented programming) and made it the way everyone programs by default. But you can't expect someone who loves C to love C++, which has almost none of the virtues that people admire in C.

    --
    Post may contain irony: discontinue use if experiencing mood swings, nausea or elevated blood pressure.
  15. Re:Syntax and typo errors compile by BitZtream · · Score: 1, Insightful

    So basically, you don't understand C, so its bad? Is that what you're saying? I'm asking because none of your examples are even mildly difficult concepts.

    You seem to have difficulty accepting the syntax for function pointers for one obvious one.

    You also don't seem to understand that a pointer and a reference are the same thing, and the array thing your referring to, is also your misunderstanding, its also passing a pointer ... because you don't pass an array ON THE STACK, you pass a reference/pointer to the array.

    Your comment about argument mismatches being a problem is easy to solve, don't use a retarded compiler, done. Now the compiler doesn't let you fuck it up. If you make a Java, C++, pascal, or .NET compiler retarded and let it ignore obviously bad code, it'll fuck up too.

    Everything in your post is just shear ignorance of the language and its syntax. You didn't even post anything about the actual difficult parts of C. You just made yourself look ... simple and uneducated, not C look bad.

    --
    Persistent Volume manager for Kubernetes - https://github.com/dwimsey/openshift-pvmanager