Designed in C and encoded in C ++

Source: Internet
Author: User
I saw Liu Jiang's blog last night and added several more sections. This morning, I saw Alibaba Cloud's Manual trackback. This topic really attracted attention.

Yun Feng first raised the question of the so-called ideological burden brought by C ++ (the "mental burden" in Classical Chinese), and then quoted Linus heavily: "The key is design ", in fact, he is suggesting that good design C can also be done without the effort of C ++. Once C ++ comes forward, it is necessary to carry additional thoughts.

I have clearly defined the State. In system-level program design, this is the case.

Don't underestimate the burden of thinking. Most, or even most, C ++ programmers cannot. On the contrary, for system-level development, C is a language that has almost no ideological burdens. To put it bluntly, it is a bit of a fortune. If you want something, you can write it. It gives you a lot of information, nothing can be done, and nothing can be left alone.

As early as N years ago, I found that writing programs was slow. I was far more familiar with STL than people around me. As an example, it should be very efficient to write programs. The result showed no. I was not confident when I was writing a program. I always thought, "I can work, but I'm afraid there is a better solution. What will it be? How can I add a template parameter? Or abstract a base class? In a bridge mode? So how can we solve the ownership problem? Who is responsible for memory recovery? Port a boost: shared_ptr! Will it slow down when multithreading is possible? It should not, but it will encounter loop references. Or do I have a weak_ptr in the middle to disconnect the loop chain? Oh, no. It's too complicated to be understood by others. Let's do it first, just work ." In this way, we came back from a circle. Sometimes, this circle is not purely in a pull-like pattern. I will really achieve a lot of "optimization" designs for comparison. At that time, the flowers will be spent on it. Sometimes, some improvements will be made, but most of the time it is not worth the candle. In my opinion, the guys next to C are just half-understood and adopt the "ctrlc-ctrlv-Modify-Debug" method, I rushed to the front of me long ago. This is the power of "mental baggage.

I have not used C ++ to write programs in recent years. I have used several languages in my spare time. After learning about some of the advantages of these languages, I can be more objective to C ++. I also thought about it, if I ran back to write C/C ++ one day, what would I do? After all, the global shortage of C ++ programmers and increasing wages still have practical significance. Just yesterday, I chatted with chensh for a while. The two people agreed that they adopted the "C + concreate class + STL" style. To put it bluntly, it is designed with C and encoded with C ++.

The reason for this is that C and C ++ are currently used for system-level development, and those gorgeous abstract mechanisms are not used. When thinking about solutions, C is used. Note that C can also be designed based on objects, objects, and components. However, when thinking about problems at the C level, the design can be lean (lean, now it's a buzzword), lighter and more direct. After the design scheme you have conceived is developed, if some of them happen to be ready-made in C ++, and using C ++ can improve the development efficiency, there are no obvious side effects, so use C ++ for the corresponding part. For example, COM was originally designed on the basis of C. During the design, it was found that the vptr + vtable with C ++ Implementation of polymorphism is actually consistent, so later, we mainly used C ++ for com development. In fact, Microsoft directly changed the C ++ compiler to meet the needs of COM development. Obviously, Microsoft first conceived the design and then made C ++ adapt to the design. Later, many c ++ programmers made the design adapt to those Language Mechanisms of C ++. in system development, this is called putting the cart before the horse. Of course, this is not so outrageous at the application level.

In fact, looking back at the early history of C ++, the earliest C ++ was built into the language of some common patterns in C, which effectively improved the development efficiency in the early days. We should look back to this spirit today.

I support STL for the same reason. Many times, the design you get from C is nothing more than something that STL has already achieved. At this time, of course, STL can be used. Especially those calculation methods are also applicable to C array. They use accumulate for summation, transform ing, adjacent_find for equal adjacent items, lower_bound and pai_range for binary search, and so on, isn't that much better than handwriting? Of course, to use STL, you must be familiar with the mechanism behind it. If you don't have this foundation, you can still use C for proper calculation.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.