C, C++ and Linus

From the previous post, it appears that basically everything in C is bad. In this talk on modern C++, most of the examples don’t contain any of the primitive elements of C. We have finally moved to a better way of programming. Don’t get me wrong, I used to like C. It was a simple language so you could learn most of it pretty quickly. You had a lot of power and knew exactly what your code was doing. However, when you try to do larger projects, it quickly becomes cumbersome. That statement is a bit general, and people have done huge projects in C, but at some point you wonder why you are forced (i.e the language doesn’t support it) to write bad code. That’s true for all languages, but especially so for C. I haven’t done C in a while so I can’t remember specifics, but here are some general things I don’t like:

  • The header/include system: you need header guards (include just dumps the file)
  • No namespaces.
  • No templates – there are things you can’t do with void pointers (which are bad anyway).
  • Macros – these are basically syntax hacking.
  • Bad string handling – scanf is unsafe, characters are assumed to be 8 bits and some functions like getchar() return an int.

People say it’s good for students to learn C first so that they know about how the computer works. Sure, learning fundamentals is good. However, I would still advise against using C because students learn all the bad practices of C and keep them for years if not their whole life.

Then I read this. I was really surprised that anyone could criticise C++. It’s something a C++ programmer would say about Java, but C++ is one of the lowest level languages there is and you should be able to do any low level stuff with it, while still having the convenience of C++ features. Here are some of the arguments:

  • C++ programmers are bad. I would have thought the opposite, especially compared to… basically any other language besides C and assembly. But it’s probably true that C programmers are better than C++ programmers in general. Although “better” in what sense? I would probably hire a C++ programmer because he/she would use the STL instead of rolling your own every time.
  • The STL/Boost is buggy and not portable – the post was written in 2007, and it’s probably improved since then. However, it is very annoying if your libraries have a bug, and if it’s not portable, your code’s going to be a mess. Although in the end, I don’t think there’s much of a choice with this. If you’re not using these libraries, you’re not going to be able to get anything done. But then again, for writing low level stuff you might not need them as much.
  • You will discover that an abstraction is inefficient and it will take a lot of work to replace it. This can be summed up as “All non-trivial abstractions, to some degree, are leaky“. However, this is much like the previous point. Are you going to write every single line of code yourself? Well, maybe his point is that the bad C++ programmer doesn’t know how it actually works. I agree that I’d like to have nice fast programs. But I would prefer a slow program than none at all. This is debatable but I’m assuming it can’t be that slow, and it is premature optimisation because I wonder how often it is the STL’s fault that your program is slow. “Are we better off with this abstraction than we were without it?” Almost always, yes. Especially because it’s the STL.
  • “… the end result is a horrible and unmaintainable mess.” – I can see a C++ program being an unmaintainable mess. But C will almost definitely be an unmaintainable mess too. This is why we need something else. An ultimate programming language that rises above this.

Linus is talking about Git, “where efficiency was a primary objective”, and in the second post, he is talking about the kernel. I guess at that level, you’re almost at assembly level already and C makes more sense. But C++ is supposed to be low level. The truth is that you end up having to “limit yourself to all the things that are basically available in C”. C is so bad, yet there is no alternative. I guess one good thing was that C is so bad that it spawned a myriad of languages attempting to solve the problems of C, but they all evolved into higher level languages and modern programming was born. Worse is Better.

Did I say no alternatives? There are actually a few languages that aim to be system languages, like Go (which has garbage collection), D (a better C++), and Rust ( the features look good). They are looking good, however I think if you can’t write a kernel with it it’s a bit questionable. Also it has to be good enough that Linus will approve of it! One day, I will port the whole kernel so we can get rid of C once and for all.