C ++ programming style

Source: Internet
Author: User
Recently, I have the opportunity to see that it is the core of the company.CodeBecause this code has always been kept confidential in the United States, what will this important code look like?
I was quite disappointed when I took a rough look, because the code style is basically the C ++ style I just graduated from-Class C, from the code alone, there is a lot of writing, there is no design pattern in it, and there is no template, the code can even write a function in nearly a thousand lines.
Such an important code is really depressing. Therefore, I THINK C ++ is better.ProgramWhat is the style?

C ++ supports a variety of model designs (process-oriented, Object-based, object-oriented, General generic, and template meta-Programming ), this makes the C ++ programming style more diverse than other languages. So some people think that C ++ is like a Swiss Army knife and has all functions. If you want to use it as a knife, it can become a knife, so it is very powerful, being powerful also means complexity. Other languages, For example, Java/C # only supports object-oriented, so that their styles are uniform. Both standard libraries, frameworks, and applications are dominated by objects, interfaces, and modes. But the C ++ program is different. It can be said that there is no fixed standard for the c ++ program style. Based on his experience and framework, each person will have a completely different style, others on the Internet summarized some C ++ program styles:

1. Classic C ++ stream: class is the core, and routines are mostly C runtime. templates are rarely used, which is generally the result of orthodox education.
2. Classical C stream: it is usually used as a C stream. It is occasionally used as an object and does not use exceptions. It prefers nostalgia.
3. MFC stream: Adhering to the MFC style, it mainly uses the MFC/ATL Object and Win32 API, does not like STL, and uses many macros to torture the IDE syntax prompt module to crash.
4. Portable stream: uses C runtime and STL as the main tool and uses classes and templates.
5. Functional stream: it uses templates and STL as the main weapon and uses a lot of functional language design methods, and claims that this is the true C ++.
6. Win32 stream: Uses global functions more, and prefers Win32 APIs, but does not exclude C runtime. Generally, lightweight programs are preferred, so they are also slim.
7. Java stream: the Java style is fully used. Global members are not allowed, but STL collection classes are allowed to write many classes called factory.
8. com stream: Like addref () and release (), a large number of interfaces are used to hide things that can be hidden. In the example, we should replace God with COM.
9. Rule stream: the pursuit of a perfect c ++ program, regardless of every const and throw (), and tries its best to avoid insecure cast. Always bring an iso c ++ manual with you.
10. chaotic stream: The program is impermanent, with no permanent path and unpredictable. I do not know its name.

The above summarizes some of our common C ++ program styles. I believe that most C ++ programmers can find their own shadows. In addition, the C ++ program style of each person is not the same, and will change with his project experience. For example, when a general person just graduated from college, the style of the Code tends to be process-oriented. Later, with the depth of object-oriented, the model and interface will be gradually used for design, at this time, the Code style tends to be object-oriented; then, it may go deep into STL and generics, or even template metaprogramming. At this time, the Code style uses template generics; in the end, some people may think that over-focusing on the object-oriented design patterns and the generic design of templates will lead people to deviate from the concerns of the problems to be solved, finally, his style is back to the original C style or the class C style just after graduation.

We can see from the above that it is difficult to set a uniform standard for the c ++ program style, but I think we can use different styles Based on the problems we want to solve. My personal opinions are as follows:

(1) The C ++ underlying language base library (STL, boost) is generic-oriented andEfficient and universalFor the design principles, I think you have reached a consensus on this. .

(2) Basic C ++ application library and framework Oriented and generic. The basic framework generally has certain requirements on scalability and performance. For the framework, we generally sum up a lot of practical experience, so we basically know all its variables, therefore, in theory, we can use the traits and Policy of template parameters to separate all possible situations. The framework itself is also complex and requires object-oriented encapsulation and decoupling, ATL is a successful example of COM component development base library. Basic FrameworkEfficient, dedicated, and scalableDesign principles.

(3) The C ++ application layer is object-oriented. The Application Layer logic is variable. In theory, you can also use template parameters to cope with changes. However, the changes in the application layer are complex and many tasks are unpredictable, therefore, you cannot predict all possible situations using template parameters. In addition, C ++ does not describe the generic concepts, which makes the template code more difficult. Using templates in a variety of application layers is obviously not a good choice. In addition, there is no mature experience in the extensive use of templates at the application layer, and object-oriented and pattern are already very mature. The application layer usesFlexible response to changesDesign principles.

(4) The interaction between C ++ modules (DLL) is dominated by C APIs or similar COM (interface + factory ). Concise and binary compatibilityDesign principles.

In short, we should flexibly apply the features of various C ++ styles and fan models, and adopt the "Multi-fan" programming approach to solve the problem, rather than using a single style.

Finally, return to my initial company's core code, which is used to solve a specific problem. Obviously, it has little to do with versatility and scalability, and thus does not need the so-called models and templates, in fact, the more you go to the underlying operating system, the farther you leave these things, so the father of Linux will give C ++ negative feedback.

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.