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:40

>>34
As an addendum, I want to say that I approve of some smart pointers as well. I say some because I believe reference counted pointers should *almost never* be used. boost::shared_ptr is currently widely used to overcome limitations in std::auto_ptr (e.g. use in stl containers), and the remaining uses are usually bad design decisions (especially wrt threading). I am looking forward to std::unique_ptr in the new C++ standard to fix a lot of these problems.

The reason why I think they're important is because the above code comparison becomes much more pronounced with heap-allocated objects (which is of course almost always the real use). Compare:

scoped_ptr<File> file(new File("somefile.txt"));
cout << file->readInt();

versus:

file_t* file;
file = (file_t*) malloc(sizeof(file_t));
file_init(file, "somefile.txt");
printf("%i\n", file_read_int(file));
file_close(file);
free(file);

The other main use for them is stack unwinding. Cleaning up the stack after a failure in C is a real pain in the ass no matter how you do it. Lots of factory functions give you a hierarchy of objects that you need to clean up when you're done (one that springs immediately to mind is opening http connections in any API since the dawn of time). With RAII and smart pointers you can just return anywhere without ugly cleanup code. Compare:

bool doStuff() {
  scoped_ptr<Foo> foo(factory->createFoo());
  if (foo->doSomething() == FAIL)
    return false;

  scoped_ptr<Bar> bar(foo->createBar());
  if (bar->doSomething() == FAIL)
    return false;

  return true;
}


as opposed to the cleanest possible way in C (and having to explain to your boss why you rightfully used a goto):

bool do_stuff() {

  int retval;
  foo_t* foo;
  bar_t* bar;
  foo = bar = 0;
  retval = 0;

  foo = factory_create_foo(factory);
  if (foo_do_something(foo) == FAIL)
    goto CLEANUP;

  bar = foo_create_bar(foo);
  if (bar_do_something(bar) == FAIL)
    goto CLEANUP;

  retval = 1;

CLEANUP:
  if (bar) {
    bar_destroy(bar);
    free(bar);
  }
  if (foo) {
    foo_destroy(foo);
    free(foo);
  }

  return retval;

}

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