Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C >= C++

Name: Linus Torvalds 2009-07-06 10:57

*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.

            Linus

Name: Anonymous 2009-07-07 1:13

In my experience with C, well-written C code tends to look like C++. Properly modularized code tends to be object-oriented by design. You have structures that maintain state, and lists of functions that operate on those structures which always take the structure as the first parameter. It looks like C++ with different method syntax.

Take any number of open pure C libraries, e.g. libpng, sqlite, etc. They all look the same way; they are all object-oriented. Libpng goes even further: it uses C++ style catch/throw. It maintains its state in its structure and longjmps to signal error, then cleans up when you destroy it.

The features that are good in C++ are the features that simplify this process: constructors/destructors, method syntax, operator overloads, and catch/throw with stack unwinding. A very small set of extensions to C.

In my experience with C++, well-written and well-behaved low-level libraries (e.g. hardware abstraction libraries) use only these features; they use very little STL and almost none of the other language features. There is really a direct translation to object-oriented C code this way (not counting stack unwinding). Compare:

File file("somefile.txt");
cout << file.readInt();

versus:

file_t file;
file_init(&file, "somefile.txt");
printf("%i\n", file_read_int(&file));
file_close(&file);

Using C++ features like constructors/destructors for RAII and operator overloading just cuts down on the boilerplate and makes code easier to read. Indeed, C++ originally started as just a preprocessor for this very reason. Personally, I would write any low-level library this way (and do in my day-to-day job, like the above File class). If you really need it, a pure C wrapper can easily be generated automatically from the resulting class definitions.

I have a completely different opinion on desktop applications though. When writing a big desktop app, you ALWAYS have a need for a dynamic array for example. For these applications I really don't give a damn how fast varying implementations of std::vector<> might be; the stability and featureset always tops rolling your own container. It just makes no sense to rewrite STL since you inevitably need to use it. Besides, since it's your own application, there's no reason not to just stick to the same implementation of STL on all platforms (e.g. libstdc++).

Same goes for most of STL, e.g. std::string versus traditional char*. Adding strings together is just *really fucking easy*, so I don't care how inefficient it is. The complexity and danger of optimizing it is just not worth it.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List