c++ productivity

Linux創始人Linus Torvalds強調在內核開發中使用C語言的重要性,並批評C++的一些特性,如過載和上下文依賴性,認為它們增加了溝通成本,降低了生產力。

Linux之父炮轰C++

--------------------------------------------------------------------------以下为原文

non2 (anon@anons.com) on 6/8/10 wrote:
>
>But productivity is a difference thing when it comes to
>kernel code. Linux devs are working practically for free.
>So the same amount of budget can get you whole lot work
>done.

Actually, this is wrong.

People working for free still doesn't mean that it's fine
to make the work take more effort - people still work for
other compensation, and not feeling excessively
frustrated about the tools (including language) and getting
productive work done is a big issue.

So if a language change were to make people much more
productive, that would be a good thing regardless of how
much people end up getting paid. It's definitely not about
the money.

But the thing is, "lines of code" isn't even remotely close
to being a measure of productivity, or even the gating
issue. The gating issue in any large project is pretty much
all about (a) getting the top people and (b) communication.

In the kernel, we have roughly a thousand people being
attributed for each and every kernel release (at
about three months apart). Now, there's a long tail, and
the hundred (or even fifty) top contributors do most of
the bulk work, but even then, the biggest issue that I end
up worrying about is not even the code, but the "flow" of
code and development.

For example, I personally don't even write much code any
more, and haven't for years. I mainly merge (and to a
large degree - don't merge: a large portion of what
I do is telling people "No, I won't take this, because of
xyz". Even if rejection ends up being the rare case, it's
actually the main reason for me existing. Anybody can say
"yes". Somebody needs to say "no").

And the best way to make things work is to not need
to communicate at all. It's exactly the same issue as in
parallel programming - any communication inevitably is the
main bottleneck.

And the best way to avoid communication is to have some
"culture" - which is just another way to say "collection of
rules that don't even need to be written down/spoken, since
people are aware of it". Sure, we obviously have a lot of
documentation about how things are supposed to be done,
but exactly as with any regular human culture, documentation
is kind of secondary.

(Put another way: there are lots of books about culture,
and you can get a PhD in anthropology and spend all your
life just studying it - but for 99% of all people, you
don't read a book about your culture, you learn it by
being part of the community).

And there is a very strong "culture" of C (and UNIX, for
that matter). And this is also where it's so important for
the language to be simple and unambiguous. One of the
absolute worst features of C++ is how it makes a
lot of things so context-dependent - which just means
that when you look at the code, a local view simply seldom
gives enough context to know what is going on.

That is a huge problem for communication. It immediately
makes it much harder to describe things, because you have
to give a much bigger context. It's one big reason why I
detest things like overloading - not only can you not grep
for things, but it makes it much harder to see what a
snippet of code really does.

Put another way: when you communicate in fragments (think
"patches"), it's always better to see "sctp_connect()"
than to see just "connect()" where some unseen context is
what makes the compiler know that it is in the sctp module.

And you have to communicate in fragments in order
to communicate efficiently. And I don't mean "efficiently"
as in network bandwidth - I mean as in "in general". The
reason we use patches instead of sending the whole project
(or even just a couple of whole files) around is not because
it's denser in email - it's because the only thing that
matters is the change , not the end result.

So that is a very fundamental reason for development to
avoid ambiguity and context. And that, btw, has absolutely
nothing to do particularly with "kernel programming". It's
true in general in any sw project, but it's true in real
life too: speaking or writing ambiguously is not good in
normal human communication either.

So a simple and clear language is a good thing. You don't
want to be unnecessarily verbose (meaningless syntactic
fluff is always bad), but at the same time you do not want
to require too much context either.

[ Lots of implicit context is fine if everybody is an
expert on the subject. Which is why really esoteric
scientific literature is basically unreadable unless
you're an expert - it requires huge amounts of context
to make sense at all. But that is simply not possible
in a large project that has many different areas.

For example, I know the VM and core kernel really well,
but I still need to be able to read the code of various
filesystems and networking code. So even for somebody
like me, the code needs to be written without hidden
context. ]

And C is a largely context-free language. When you see a
C expression, you know what it does. A function call does
one thing, and one thing only - there will not be some
subtle issue about "which version" of a function it calls.

Of course, you can use the preprocessor and inline functions
to do that, but even then you have to be pretty explicit:
you can still grep for that preprocessor symbol, and it's
all pretty "direct".

Now, in other situations you do want more language support,
and you do want the language to do memory allocation etc
for you (ie GC - I'm not talking about that idiotic
"new" keyword in C++, or other crap). In the kernel, we
couldn't do that anyway. Similarly, in the kernel, we do
really require very specialized locking and direct control
over memory ordering etc, so a language that exposes some
model of concurrency would almost certainly be too limited
in that concurrency too.

So there are particular reasons why I think C is "as simple
as possible, but no simpler" for the particular case of an
OS kernel, or system programming in particular. That's why
I'm absolutely not saying that you should use C for all
projects.

But C++? I really don't think the "good features" of it
are very good at all. If you leave C behind, do it properly
and get some real features that matter. GC, some
concurrency support, dynamic code generation, whatever.

Linus

### C++ Standard Library Utilities Functions The C++ Standard Library includes a variety of utility functions and tools designed to simplify programming tasks by providing reusable code components. These utilities are part of the `<utility>` header file, which contains definitions for general-purpose templates that provide support for pairs, tuples, move semantics, type traits, and more. #### Common Utility Components 1. **std::pair**: This template class represents a pair of values as one object. It allows storing two heterogeneous objects within a single unit. ```cpp std::pair<int, double> myPair(10, 3.14); int firstValue = myPair.first; // Accessing the first element double secondValue = myPair.second; // Accessing the second element ``` 2. **std::tuple**: A tuple is an extension of `std::pair` allowing multiple elements with different types stored together in a single structure[^1]. ```cpp std::tuple<int, double, std::string> myTuple(42, 3.14, "hello"); int value1 = std::get<0>(myTuple); // Retrieve the integer component double value2 = std::get<1>(myTuple); // Retrieve the double component std::string value3 = std::get<2>(myTuple); // Retrieve the string component ``` 3. **Move Semantics (`std::move`, `std::forward`)**: Introduced in C++11, these utilities enable efficient transfer of resources between objects without unnecessary copying operations[^2]. They play a critical role in optimizing performance when dealing with large data structures such as strings or vectors. ```cpp std::vector<int> sourceVector{1, 2, 3}; std::vector<int> destinationVector = std::move(sourceVector); // Transfers ownership instead of copying ``` 4. **Type Traits**: Type traits allow querying properties about types at compile time, enabling conditional compilation based on those queries. Examples include checking whether a given type is integral, floating-point, pointer, etc. ```cpp static_assert(std::is_integral_v<int>, "int must be an integral type."); ``` 5. **Swap Functionality**: Provides mechanisms for swapping contents efficiently across various containers like arrays, lists, maps, sets, among others. ```cpp std::list<int> myList1 {1, 2}, myList2 {3, 4}; myList1.swap(myList2); // Swaps content directly rather than reassigning each item individually ``` 6. **Exchange Operation**: Allows replacing existing variable's value while returning previous state simultaneously using function named exchange()[^1]. ```cpp int originalVal; int newValue = std::exchange(originalVal, 789); // Sets 'originalVal' to 789 & returns old value before assignment took place ``` These functionalities form just some examples illustrating how versatile yet powerful modern-day implementations provided through updated standards continue enhancing developer productivity significantly over traditional approaches available earlier versions only limited capabilities compared against current offerings today!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值