时间:2021-07-01 10:21:17 帮助过:11人阅读
On Wed, 5 Sep 2007, Dmitry Kakurin wrote:
>
> When I first looked at Git source code two things struck me as odd:
> 1. Pure C as opposed to C++. No idea why. Please don't talk about portability,
> it's BS.
*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
首先,我想用两段"很长的代码"来做一个开始。#include
/*
* min()/max() macros that also do
* strict type-checking.. See the
* "unnecessary" pointer comparison.
*/
#define min(x,y) ({ \
typeof(x) _x = (x); \
typeof(y) _y = (y); \
(void) (&_x == &_y); \
_x < _y ? _x : _y; })
int main(void)
{
int a = 3, b = 5;
double d1 = 3.14, d2 = 2.718;
printf("min of a & b: %d\n", min(a, b));
printf("min of d1 & d2: %lf\n", min(d1, d2));
return 0;
}
这个问题也是当时困扰我的,为什么Linux kernel没有引入C++. 各甚用C++重写,收藏的一个回答。The reasons specifically for Linux are explained in great detail as
part of the Linux Kernel Mailing List (LKML) Frequently Asked
Questions (FAQ) at:
http://www.tux.org/lkml/
Scroll down to section 15 (titled Programming Religion) and read the
explanation. A summary is:
- Linus started with an 386 computer, Minix, and gcc (no g++ available)
- C is used instead of Assembly for a LOT of reasons (maintenance,
readability, efficiency, ...)
- it is not clear how an object oriented language helps in OS design
- MANY years ago, they tried using g++ to compile the Linux kernel
and found it ran slower than when compiled with gcc (many thought it
should have been the same); they are not willing to do this again
- Linus makes the final decision and he's decided to stick with C
There is also another good explanation of some of the issues at
http://kerneltrap.org/node/2067
which describes one person's attempt to build adapt some C++ code to
incorporate into a kernel module.
For a historical perspective, Thompson and Ritchie developed Unix
using some of the concepts from Multics and CTSS
http://www.multicians.org/unix.html
including implementation of most operations in a higher order language
(Unix - C, Multics - PL/I), common naming of commands, etc. I also
refer you to
http://cm.bell-labs.com/cm/cs/who/dmr/hist.html
which describes the EARLY development of Unix including the conversion
from Assembly on the PDP-7 to C on the PDP-11 (near the end of the
paper).
Building from that basis, the Unix from AT&T that was widely
distributed and worked on by thousands of people was a C based
operating system. In the 1970's and 1980's, an alternative to AT&T's
distribution was developed at UC Berkeley, see
http://en.wikipedia.org/wiki/Berkeley_Software_Distribution
for a relatively short explanation of the development of BSD. The BSD
version eventually did not use ANY of the AT&T code base, but was also
implemented in C. At the end of the article is a list of several BSD
descendants including SunOS (now Solaris).
That is not to say that some applications are better implemented in an
OO language such as C++. There are several Graphical User Interface
(GUI) toolkits such as KDE which are implemented in C++. However,
there are applications that are better suited for a more procedural
language, and at this point operating systems are in that category.
If this answer is unclear or somehow incomplete, please make a
clarification request.
Good luck with your work.
--Maniac
Linus不喜欢C++
更底层,更可靠,更稳定。
问Linus吧,看看他会不会喷你一脸(▭-▭)✧
c++太自由了。写操作系统内核这东西,你放心么?