Project In C Coding Practice
In practice using C programming, there will be some deficiencies that affect program performance, efficiency, localization, etc., which will list some of the defects for everyone's reference, and give some adverse defects, and due to defects The degree of influence is given to give a defect look priority, 1 ★ The priority is the lowest.
1, prompt information in the program
The prompt information in the program and the code of the program are mixed.
Defect Find Priority: ★
Description: Write the prompt information in the program in the code, which will not be conducive to the localization of the program. For example, the program prompt information is in English, and is written in a CPP file. After the compilation is completed, it is unclear which is code, which is the prompt information, which is very inconvenient to this. The solution is: If you are programming with VC , you can put all the string constants used in the program in the String Table of the resource file. You can use CString :: LoadString (NID) from the resource file from the resource file when you use the String Table. So when you need Chinese, just translate the prompt information in the String Table into Chinese. If you don't use VC to develop programs, you can put these string constants in a text file, and you can use the contents of this text in this text.
In addition, the prompt information and code mixing in the program also add to errors in the program, which is not conducive to the modification of the program. Separate the program code and the prompt information, you can reduce the number of errors when writing large software.
2. Enter information acquisition
When you get the input information, you often need to specify a buffer that accepts the input. This is a defect directly using the buffer to store the input information.
Defect look priority: ★ ★
Note: If you do not judge or limit the length of the input, a problem of buffer overflow may result in the crash of the system.
3.i / o operation
The operation results are not checked when the I / O operation is performed.
Defect look priority: ★ ★
Note: If the I / O operation result is not checked, the exception generated by I / O will cause the program's crash, causing dissatisfaction with the user. For example, to open a file, but do not make a judgment on the file, if this file does not exist, or there is no permission to use this file, then the program will exceed the program.
4. Constants used in the program
The value of the constant is used directly in the program.
Defect look priority: ★ ★
Note: The large number of constant values embedded in the program make the program difficult to maintain, and it will bring difficulties to the debugging of the program. Preferably, the constant is defined constant variable const.
5. Efficiency in COUT
When using COUT output, a large number of ENDLs are used to wrap.
Defect Find Priority: ★
Note: When COUT outputs to the standard device, corresponding to a memory buffer, the use of ENDL clears the buffer, frequent use of ENDL reduces the efficiency of the program, should be written as "/ n".
6. Functions that can fail
For functions that may fail, do not check the operation results.
Defect look priority: ★ ★
Explanation: For example, allocate a memory, it is possible to assign the required memory space. For this potential operation failure, it is not checked, and the program will continue, which will cause an exception or even crash. Be sure to make a judgment on all potential operation failures.
7. Include and Namespace Abuse
Repeats some files or use namespaces or contain unnecessary files.
Defect Find Priority: ★
Note: Repeated use of include and Namespace, this is a defect, namely, multiple use or if it should not be used. For example, a class has already in its header file, and in the .cpp file, IString.h in the .cpp file, which is in the program in the program, which may appear in the program. Duplicate definition error. Even if it is not wrong, it will also cause naming pollution and increase the cost of compilation. The so-called pollution is when it comes back and forth, it may have a place to modify, and another place forgets to modify, so the program will have problems. It is similar to the paradigm in the database to eliminate redundancy, and the program must avoid data redundancy. 8. Inheritance
Take the use of other class features as a inheritance.
Defect look priority: ★ ★ ★
Note: Use inheritance relationships do not necessarily reflect the relationship between classes and classes, and sometimes may lead to confusion of the program structure, only IS-A can use inheritance. If you just use another class, it is not appropriate to define the relationship between classes and classes. This time is the relationship between HAS-A, or the overall relationship, and the inheritance will only lead to confusion. In addition, the inheritance relationship will also lead to another problem, such as:
Class x {
Y y;
}
This structure typically appears in the x.h file, and must be written in #include "y.h" at the beginning of this file, so that class X is tightly coupled together. After the program is finished, if Y.H changed, y.h changed, inevitably caused X.H to change, so that all files that include "X.h" have changed, causing a series of chain reactions. The current compiler is usually used in an incremental compile, that is, only the part of the file has been modified. Therefore, even in this procedure, even the small part of Y.H is modified, it will also cause a lot of file changes, and the compiler compiles these large variable files, which will consume a lot of time.
To avoid the Solution of Include "Y.h" in x.h as follows:
Class Y;
Class x {
Y * Y; // Y is the pointer of Y, accounting for 4 bytes
X () {
y = new y;
}
}
Here, you must use the form of Y * Y, otherwise you will have problems directly using Y Y. Because each time a X-based instance X is generated, the compiler is allocated to the memory space, while also understanding how much the space occupied by the instance y is. And if you want to know how much Y is y, you must know the definition of Y, so you must have include "y.h". In this program, the pointer is used, and the pointer always only accounts only 4 bytes in the Windows system. The compiler always knows how much memory should be allocated for x, so I just need to know Y is a Class Foot, but not You need to know the specific definition of Y.
Go back to the above question, why it is easy to generate a problem when it defines inherits, because the inheritance is actually an instance of a base class at the top of the derived class instance, such as:
Class X: Public y {// Place an instance of a base class, be sure to assign space to the base class
(Y y;)
......
}
Since it is necessary to put a base class, then only know how much this instance is, in order to assign memory to the instance of the derived class, the truth is equal to the above. So inheriting this relationship, it must lead to the head include "y.h", which will inevitably lead to the above problems, which is also a disadvantage that the object-oriented development method is. A large difference between the component-oriented software development method and the object-oriented software development method is that the component-oriented software development method discards one of the object-oriented software development methods, which is complex inheritance relationship to make the program tightly coupling Together. The development method of the component defines a clear interface for each component, and interacts with an interface between the components, so that the coupling is very low. 9. Lost of error message
There is no detailed record for the cause of an error in the program, or the value is simply used to represent the error message, resulting in the loss of the error message.
Defect Find Priority: ★
Note: The details of the error can be better able to find the cause of the error, and when the user uses an error, it can easily reproduce the error according to the details. Therefore, when each program may be wrong, a precise error code should be returned, which is conducive to debugging and maintenance.
10. Type conversion
Use C-style forced type conversion.
Defect look priority: ★ ★
Description: The C-style mandatory type conversion, such as a (int) variable name, this type of conversion is difficult to locate, which is not conducive to finding mandatory type conversion in the code, thereby reducing the maintenanceability of the program. The mandatory type conversion operator Static_cast, Dynamic_cast, const_cast, reinterpret_cast, which can be used, so you can find the type conversion in the program via the keyword CAST.
11. For types of types
The type definition is not performed according to the real value range of the data, and the inappropriate data type is used.
Defect look priority: ★ ★
Explanation: For example, use INT type to define age, and age does not have a negative number, so that the value range of data is expanded, and storage space is wasted. At the same time, the complexity of judgment of data validity is also increased.
12. Redundant definition
Redundant definitions have been redundant for data types.
Defect Find Priority: ★
Description: The definition of redundancy for data types and constants will cause the program to be difficult to maintain. For example, the template class data type general definition form is type
13. Redundant code
The same code to complete the same feature has repeatedly repeated.
Defect look priority: ★ ★
Description: The same operation is not written twice in the program, which increases the possibility of an error, which is not conducive to the maintenance of the program.
14. Memory allocation and recycling
A large number of work that cause memory allocation and recycling, such as New, Delete, and object copy.
Defect look priority: ★ ★ ★
Description: The allocation of memory is a very time-consuming thing, and a large number of uses will result in the efficiency of the program. For example, don't go to dynamic application (New) in a small memory every time. If you know that the memory space that may be used, you can apply for a large memory one-time.
15. Values used in the program
In the program, when you need to use some values to control the process of the program, the number is used directly.
Defect look priority: ★ ★
Explanation: Direct use of numbers can cause programs to be difficult to understand and maintain. For example, for (i = 1; i <10;) This statement is not very good, it should be written as follows:
const marxnum = 10; for (i = 1; i Because if there are numbers that are directly used everywhere, it is very difficult to force when you need to modify it. 16. Selection of data structure The inappropriate data structure is selected. Defect Find Priority: ★ Explanation: Selecting an inappropriate data structure causes the efficiency of the program to be affected, extending the running time of the program. 17. Is the prefix or suffix? The suffix expression can be used if the prefix and suffix self-gase can be completed. Defect Find Priority: ★ Note: Use suffix expressions to be efficient because a temporary variable is generated in the suffix expression operation, and this temporary variable is an additional time and overhead.