Get the App
SLTechnology News&Howtos  ›  Development  › 

How to program C++ code

Shulou Source: shulou.com Published: 2022-06-02 22:38:51 09月25日 Update

This article shows you how to code C++, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.

For users and scholars who have come into contact with C++ coding for many times, it is very important to understand the concept of C++ language, so let's first talk about what C++ language is. The so-called C++ language: it is a very widely used computer programming language.

In our actual development, we often reuse the code by copying the C++ code. This includes a copy of a piece of code, or copies of several files. Instead, I would like to propose what I think is the most basic coding principle: principle 6, never copy code unless you have to.

If we copy the code inside a project, it means that some of the code inside the project must be duplicated. As an efficient developer, why write repetitive code instead of reusing them directly? This shows that there is something wrong with the design of the code, or that the developer has made perfunctory operations for the sake of temporary convenience.

If we copy C++ code from one project to another. It means that we have actually written code that can be used between projects. This kind of code has been tested by at least one project, so why don't we just use it instead of copying it? The copy of the code has at least the following disadvantages:

1. If this code does not have bug. Then bug may appear during the copy process.

two。 If the code has bug, then bug is copied during the copy process. Bug can infect other engineering components, and even other engineering projects.

The so-called code reuse, I intend to give a definition as follows:

Bool check (const vector

< condition * >

& conditions) {unsigned int i; bool result = true; for (iConditions [I]-> check_type = = "Hardware") resulte & & = HardwareCheck (condition- > param1,condition- > param2); else if (conditions [I]-> check_type = = "Registry") resulte & & = RegistryCheck (condition- > param1,condition- > param2); else if (conditions [I]-> check_type = = "OS") resulte & & = OSCheck (condition- > param1,condition- > param2) Else if (conditions [I]-> check_type = = "Process") resulte & & = ProcessCheck (condition- > param1,condition- > param2); ... }}

The above if... Else if is not only ugly but also long. More importantly, it is very unscalable. This check component must rely on a series of modules with very complex implementations, such as HardwareCheck, RegisterCheck, OsCheck, and ProcessCheck, which cannot be operated without any one of them. In implementation, this check is not reusable at all.

In addition, if there is a # include def.h in Log.c, it is destined not to be easily "copied". This is a convenient consideration in the engineering development phase: suppose I include all the header files, macro definitions, or function declarations in a header file called def.h. Well, it's very convenient for me to write C++ code, usually as long as # include "def.h", without worrying about the lack of header files.

The above content is how to code C++. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Code copying caching engineering documents languages reuse development programming importance content principles practice skills knowledge components processes problems complexity conciseness Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB macOS NVidia OPPO Reno Shulou Technology