Why c ++

Source: Internet
Author: User

Problem

Why c ++? Before you frown and close the webpage, try to answer such a simple question.

The answer is efficiency, right? Everyone knows the answer. However, we should discuss a programming language or related things from a more professional perspective. So let me ask you another question: efficiency is
No, it's the only reason people choose to use C ++. Why don't they use C? C is generally more efficient than C ++ (of course, I know, it has proved that C is not more efficient than C ++ to some extent, but please do not
If they are equivalent, the problem still exists ).

Myth

I know that you may say that this is a "preferred choice", because after all, C ++ is designed as the optimization of C and the expansion of C. Maybe it is not as efficient as you think, but at the same time, it has many fantastic high-level features. Then the problem comes down"Do developers really need these fantastic features ?"
I mean, after all, we have heard of Kiss (Keep it simple, stupid! Keep it simple) and stuff (material), we have heard of this saying-C is more kiss than C ++, so we should choose C.This endless debate turned the comparison between C and C ++ into a myth (or a mess ).
Surprisingly, it seems that many people prefer C, and the reason is that C ++ is too difficult to use correctly. Even Linus thinks so.

The real serious impact of this phenomenon is that when more people are forced to weigh the pros and cons between C and C ++, they choose C. Once they start to use C, they will soon feel satisfied and comfortable
Server is a "satisfactory" experience. In this way, when the debate arises, they will come up to say that C is a better choice than C ++. In fact, they have never really tried to use
C ++, or they are not good enough C ++ programmers. The real answer often begins with"It depends on
".

So what do I say "It depends on? Obviously, C is better than C ++ in some fields. For example, the development of device drivers usually does not require OOP/GP (Object-Oriented Programming/conceptual programming) technology. It only requires simple data operations. The most important thing is that programmers can correctly understand how the system works and what they should do. Considering the development of OS (operating system), I have never participated in the development of any OS, but I have read a lot of OS code (most of which are UNIX ), I feel that the development of many important parts of the OS does not require OOP/GP technology.

However, this means that C is better than C ++ in all fields that emphasize efficiency?Actually not
.

Answer

  Let's analyze specific issues

First, when people care about efficiency, they actually care about two types of efficiency: time efficiency (for example, OS, runtime, real-time software, and high-requirement systems) and space efficiency (for example: all Embedded Systems
System ). However, such classification does not really help us decide whether to choose C or C ++, because C and C ++ are very efficient in time and space. Which language does it really affect?
Between C ++) is the business logic (here "business" does not refer to "enterprise application business "). For example, whether it is better to use OOP/GP to express the logic, or whether to consider data and programs as well as to keep the software beautiful.

From this point, we can blur the application into two categories (of course, the premise is that we only care about C/C ++, and do not care about Java/C #/Ruby/Erlang ): low-level applications and high-level applications. Low-level applications mean that there is no need for those dream abstractions, such as Ob (Object-based), oop, and GP here. High-level applications are of course required. Apparently,In all fields requiring C/C ++ (due to their high efficiency), there are a large number of "high level" applications
(See the list on the Bjarne stroustrup homepage). c ++ is more useful in these fields.

But from another perspective,Even in these fields, programmers do not use high-level abstractions in their code, but there is still a reason they should use C ++.
Why?Because your code does not use classes and templates, it does not mean that you do not use class libraries.
Considering the practicality of all convenient C ++ class library tools (which will soon have calibration extensions tr1/tr2), I think in these cases, there are very good reasons to choose C ++ -- encoding when you can still use the C form (to maintain the kiss in any way you want ). You can also use powerful C ++ class libraries (such as STL standard template library and tr1/tr2 component ). In the end, we will find that this may be ignored by many people --Sometimes kiss relies on abstraction
. I think Matthew Wilson explained this point thoroughly in the preface to his new book "extended STL, vol1. The book mentions two pieces of code, which are written in C and C ++ respectively:

 

// C code
Dir * dir = opendir (".");
If (null! = DIR)
{
Struct dirent * de;
For (; null! = (De = readdir (DIR ));)
{
Struct stat st;
If (0 = Stat (de-> d_name, & St )&&
S_ifreg = (St. st_mode & s_ifmt ))
{
Remove (de-> d_name );
}
}
Closedir (DIR );
}

// C ++ code
Readdir_sequence entries (".", readdir_sequence: files );
STD: for_each (entries. Begin (), entries. End (),: Remove );
And it's even simpler in C ++ 09:
// C ++ 09 code
STD: for_each (readdir_sequence (".", readdir_sequence: files),: Remove );

I think, it is clear, why c ++ should be used even if people do not need classes and templates -- you will find out how useful the convenient C ++ class library is. Similarly, if
An efficient container (or a clever pointer) can free you from all the hard work of manual memory operations. What are the reasons for using the original malloc/free? If a good
String class (I'm not talking about STD: string; everyone knows this is not the best C ++ job) or the RegEx class can help you get rid of all the messy strings you don't want to see.
What is the reason for manually doing this? If a "transform" (or 'for _ Each') statement can complete your work in a simple and clear line
(Of course, I know that C ++ requires the support of Lambda functions to do this). So, why do I need to manually write for-loops? If highly customized functions can really achieve what you want
Yes, so what is the reason to use clumsy workspace to complete the same transaction?

  Kiss does not mean "rough"; kiss means choosing the most suitable tool for your work, and "best" means that the tool you use can be as direct (and concise) as possible) to help you express your thoughts.
As long as it does not affect the readability and readability of the Code.

  Practical problems

People may say that C ++ is prone to errors. On the contrary, C is usually easier to manage and manipulate. A medium-level C ++ programmer may write a large string of closely related classes, and soon these classes will become a pile of garbage. But this is actually only a few cases. On the one hand, this often happens in any object-oriented language.There are always some programmers who dare to define classes on top of the class, and they don't even know what is HAS-A and IS-A;
After learning all the syntax for defining a class and inheriting a class from other classes, they feel that they have mastered the essence of OOP. On the other hand,Why does the problem occur in C ++, because C ++ has a lot of complexity to block the design, because C ++ is so flexible, as a result, every problem solved with C ++ has many optional solutions (considering all gui class libraries), so it is difficult to weigh the solution to be selected.
C ++
The ancillary complexity is a legacy problem. c ++ 0x has made many attempts to get rid of this problem; flexibility in design is not a bad thing-if you consider it, it can help good designers make good decisions.
If some people condemn it, because it wastes a lot of brain cells, it is just a personal problem, not a language problem. Maybe such people should not be responsible for design. If you are worried about your c ++ Programming
Your partner is tempted by these high-level features to cause a mass of engineering code,Maybe you should establish an encoding standard and enforce it
(Or you can follow the collective wisdom, adhere to the C specification, or have C ++ Class C specifications), rather than giving up because of risks (policies can avoid risks ),If you do not do this, you will no longer be able to access all c ++ class libraries.
.

  On the other hand, there are the most important psychological problems
-- If a language has a strange nature, someone will eventually discover it, and then people will be attracted to it, this will attract the energy (a bit like Murphy's Law) of those who are trying to do something useful and not disturb those who are working on a perfect solution.People are naturally attracted by some rare resources. The conclusion is: the odd and strange nature is rare resources, so it will attract people's attention.
You don't have to worry about a trick to make people feel different. The worst thing is that even the secrets of no value will attract people's strong attention.

 How many skills are there in C ++? How many tricks are there in C ++? In short, how complicated is C ++?

To be fair, most tips and techniques have been discovered in recent years (for example, modern
C ++), has been used in real needs, especially to achieve high flexibility and attribute class library components (consider all the components in boost ). They actually (to some extent) guide some reality
The perfect solution to the problem. You can consider this as follows: if you are in such a situation, you have to use tips to implement something really useful; or if you do not use tips to implement it, then others will not
It will benefit from using it. Which one do you choose? I think smart people will choose the previous one. No matter how difficult the tricks are, how troublesome the implementation is.

However, all arguments cannot change the fact that:That is, we deserve a language that can cleanly express our ideas in the code.
Taking boost. function/Boost. Bind/Boost. tuple as an example, variadic templates (variable template) can easilyReduce the number of communication lines to a tenth
)
Implement these three (more in the future) class libraries, and the code will become concise and as simple as possible. Auto, initializer-list, rvalue-reference,
Template-aliasing, strong-typed enums, delegating-constructors,
Constexpr, alignments,
Inheriting-constructors, etc. All these C ++ 0x features share a common goal --Remove the complexity or obstacles in the language.
.

Like Bjarne
As stroustrup said, C ++ is obviously too complicated; obviously, people fear it and give up on it. However, "people also need relatively complex languages to deal with absolutely complicated problems ". We cannot decrease
Language features to make the language more powerful. Complex features such as templates or more complex multi-inheritance may be more useful for your needs. You only need to understand them very seriously and necessary, so that
It won't lift a stone and hit its own feet.Among all the complexity of C ++, the only thing that hinders us is the affiliated complexity.
(Someone calls it a "barrier"). It is not an example supported by the Language (only three are supported ). This is why we need to enhance C ++ 0x,Because its goal is to remove the conformances of C ++ in the long term, so that all secrets can be integrated.
(The number of such things is absolutely large. You can refer to the books about C ++ or the C ++ root database and you will understand what I'm talking about ),In this way, we can express our ideas clearly and directly.
.

  Conclusion

C ++ is difficult and difficult to use correctly. So when you decide to use it, be careful and be sure to know where you are and what you really want. The following is a simple guide:
Are they efficient? If necessary, do we need to abstract our code (this issue must be carefully considered, it is difficult to estimate whether the benefits of using the high level features of c ++ exceed the correct usage of it.
Their risks; the correct answer depends on how well your programming skills are trained, what encoding standards do you follow, and how well these standards are enhanced, etc )?
If necessary, use C ++. Otherwise, do we need c ++ class libraries to reduce our workload? If necessary, use C ++, but remember what you are doing-if your code does not really need all the dreamy abstractions, do not barely use them;Do not use classes or templates only because the code you write is. cpp.
.

Otherwise, C ++ is used, but you may wonder why C ++'s c encoding core is not used. The same reason: people are easily confused by strange language features, even if they do not know
Are these features helpful to them? I can tell you with no annoyance that I have written a series of classes to find out what these classes are ". Therefore, if you can stick to the C Core or
C ++'s c core, and keep things simple (kiss); or if your code needs to be transplanted from C to C ++, use C ++! But be careful. On the other hand, if your code
You do not need high-performance features or C ++ class libraries, because what you need to do is so simple that you do not even need convenient components such as containers or strings; or you
I think the benefits of using C ++ in your project are not worth your risk; or you just don't have enough people who can use C ++ correctly, then you should stick to C.

The most important thing is to keep things simple (but remember that such simplicity can be achieved through the use of High-level class libraries); when necessary (even if necessary, it should be used less; follow a good design principle, use abstraction.

Related Article

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.